View Code of Problem 131

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main(void)
{
    int lesson[1001][1001], degree[1001];
    int n, m;
    while (cin >> n >> m)
    {   
        memset(lesson, 0, sizeof(lesson));
        memset(degree, 0, sizeof(degree));
        for (int i = 0; i < m;i++){
            int a, b;
            cin >> a >> b;
            lesson[a][b] = 1;
            degree[b]++;
        }
        int cnt = n,last;
        while(cnt){
            cnt--;
            last = -1;
            for (int i = 1; i <= n;i++){
                if(degree[i] == 0){
                    last = i;
                }
            }
            if(last == -1 && cnt){
                cout << "ERROR" << endl;
                break;
            }
            degree[last] = -1;
            for (int i = 1; i <= n;i++){
                if(lesson[last][i] == 1){
                    lesson[last][i] = 0;
                    degree[i]--;
                }
            }
        }
        if(cnt == 0){
            cout << "RIGHT" << endl;
        }
    }
}

Double click to view unformatted code.


Back to problem 131