View Code of Problem 131

#include<bits/stdc++.h> 
using namespace std;
int n,m;
int M[1005][1005];
int in[1005];
int main(){
	
	while(cin>>n>>m){
		memset(in,0,sizeof(in));
		memset(M,0,sizeof(M));
		for(int i=0;i<m;i++){
			int a,b;cin>>a>>b;
			M[b][a]=1;
			in[a]++;
		}
		stack<int>s;
		int num=0;
		for(int i=1;i<=n;i++){
			if(in[i]==0){
				s.push(i);
				num++;
			}
				
		}
		
		while(!s.empty()){
			int cur=s.top();
			s.pop();
			for(int i=1;i<=n;i++){
				if(M[cur][i]==1){
					in[i]--;
					if(in[i]==0){
						s.push(i);
						num++;
					}
				}
			}
		}
		if(num==n&&s.empty()){
			cout<<"RIGHT"<<endl;
		}
		else{
			cout<<"ERROR"<<endl;
		}
		 
	}
}
 
 

Double click to view unformatted code.


Back to problem 131