【拓扑排序】CODEVS 2833 奇怪的梦境

拓扑排序模板。

 1 #include<cstdio>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 #define N 10001
 6 vector<int>G[N];
 7 stack<int>S;
 8 int n,m,x,y,ru[N],tot;
 9 int main()
10 {
11     scanf("%d%d",&n,&m);
12     for(int i=1;i<=m;i++)
13       {
14         scanf("%d%d",&x,&y);
15         G[x].push_back(y);
16         ru[y]++;
17       }
18     for(int i=1;i<=n;i++)
19       if(!ru[i])
20         S.push(i);
21     while(!S.empty())
22       {
23         int U=S.top(); S.pop();
24         tot++;
25         for(vector<int>::iterator it=G[U].begin();it!=G[U].end();it++)
26           if(!(--ru[*it]))
27             S.push(*it);
28       }
29     if(tot==n) puts("o(∩_∩)o");
30     else printf("T_T\n%d\n",n-tot);
31     return 0;
32 }
posted @ 2014-11-06 11:26  AutSky_JadeK  阅读(230)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト