首页 > 编程语言 > 详细

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

时间:2014-11-06 12:30:44      阅读:544      评论:0      收藏:0      [点我收藏+]

拓扑排序模板。

 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 }

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

原文:http://www.cnblogs.com/autsky-jadek/p/4078290.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!