Sicily1424. 奖金

拓扑排序

// Problem#: 1424
// Submission#: 1213091
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

vector<int> adj[10005];
int indeg[10005];
int main()
{
    int n,m,a,b,ans=0,sorted=0;
    cin>>n>>m;
    ans=n*100;
    memset(indeg,0,sizeof(indeg));
    for(int i=0;i<=n;i++)
        adj[i].clear();
    for(int i=0;i<m;i++)
    {
        cin>>a>>b;
        adj[b].push_back(a);
        indeg[a]++;
    }
    queue<int> q;
    int ok=0;
    while(true)
    {
        for(int i=1;i<=n;i++)
        {
            if(indeg[i]==0)
            {
                indeg[i]=-1;
                q.push(i);
            }
        }
        if(q.empty())break;
        while(!q.empty())
        {
            int num=q.front();
            q.pop();
            sorted++;
            for(int i=0;i<adj[num].size();i++)
            {
                indeg[adj[num][i]]--;
            }
            ans+=ok;
        }
        ok++;
    }
    if(sorted==n)
        cout<<ans<<endl;
    else
        cout<<"Poor Xed"<<endl;
    return 0;
}                                

你可能感兴趣的:(sun)