View Code of Problem 131

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
vector<int>graph[1005];
int indegree[1005];
bool topoSort(int n) {
	stack<int>node;
	for (int i = 1; i <= n; i++) {
		if (indegree[i] == 0) {
			node.push(i);
		}
	}
	int count = 0;
	while (!node.empty()) {
		int v = node.top();
		node.pop();
		count++;
		for (int i = 0; i < graph[v].size(); i++) {
			int u = graph[v][i];
			indegree[u]--;
			if (indegree[u] == 0) {
				node.push(u);
			}
		}
	}
	return n == count;
}
int main()
{
	int n, m;
	while (cin >> n >> m) {
		memset(graph, 0, sizeof(graph));
		memset(indegree, 0, sizeof(indegree));
		for (int i = 0; i < m; i++) {
			int from, to;
			cin >> to >> from;
			graph[from].push_back(to);
			indegree[to]++;
		}
		if (topoSort(n)) {
			cout << "RIGHT" << endl;
		}
		else {
			cout << "ERROR" << endl;
		}

	}

	
	return 0;
}

Double click to view unformatted code.


Back to problem 131