hdu 3342 Legal or Not

简单的拓扑排序。

#include<cstdio>

#include<cstring>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

int rudu[105];

vector<int>abc[105];

int main()

{

    int n, m, i, x, y, j, k;

    while (~scanf("%d%d", &n, &m))

    {

        if (n == 0 && m == 0) break;

        int sum = 0;

        memset(rudu, 0, sizeof(rudu));

        for (i = 0; i < 104; i++) abc[i].clear();

        for (i = 0; i < m; i++)

        {

            scanf("%d%d", &x, &y);

            rudu[y]++;

            abc[x].push_back(y);

        }

        for (i = 0; i < n; i++)

        {

            for (j = 0; j < n; j++)

            {

                if (rudu[j] == 0)

                {

                    sum++;

                    rudu[j]--;

                    for (k = 0; k < abc[j].size(); k++) rudu[abc[j][k]]--;

                    break;

                }

            }

        }

        if (sum == n) printf("YES\n");

        else printf("NO\n");

    }

    return 0;

}

 

你可能感兴趣的:(HDU)