uva10305(拓扑排序)

题目连接:UVA - 10305 

拓扑排序

lrj167

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<set>
 4 #include<iostream>
 5 #include<cctype>
 6 #include<string>
 7 #include<sstream>
 8 #include<algorithm>
 9 #include<map>
10 #define LL long long
11 using namespace std;
12 const int maxn=110;
13 int u,v;
14 int n,m;
15 int t;
16 int top[maxn],vis[maxn];
17 int p[maxn][maxn];
18 int  dfs(int u)
19 {
20     vis[u]=-1;
21     for(int v=1;v<=n;v++) if(p[u][v])
22     {
23         if(vis[v]<0) return 0;
24         else if(!vis[v]&&!dfs(v)) return 0;
25     }
26     vis[u]=1;
27     top[t--]=u;
28     return 1;
29 }
30 int main()
31 {
32     while(scanf("%d%d",&n,&m)&&(n||m))
33     {
34         memset(p,0,sizeof(p));
35         memset(vis,0,sizeof(vis));
36         for(int i=0;i<m;i++)
37         {
38             scanf("%d%d",&u,&v);
39             p[u][v]=1;
40         }
41         t=n;
42         for(int i=1;i<=n;i++) if(!vis[i])
43             dfs(i);
44         for(int i=1;i<n;i++)
45             printf("%d ",top[i]);
46         printf("%d\n",top[n]);
47     }
48 }
模板
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int G[101][101],c[101],top[101],top1;
 6 int a,b;
 7 bool dfs(int u)
 8 {
 9     for(int m = 1;m <= a;m++) if(G[u][m])
10         if(!c[m]) dfs(m);
11     c[u] = 1;
12     top[top1--] = u;
13     return true;
14 }
15 int main()
16 {
17 
18     while(cin >> a >> b && (a || b))
19     {
20         int x,y;
21         top1 = a;
22         memset(G,0,sizeof(G));
23         memset(c,0,sizeof(c));
24         while(b--)
25         {
26             cin >> x >> y;
27             G[x][y] = 1;
28         }
29         for(int m = 1;m <= a;m++) if(!c[m])
30             dfs(m);
31         for(int m = 1;m <= a;m++)
32             cout << top[m] << ' ';
33             cout << endl;
34     }
35     return 0;
36 }
简化版(无环)

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<stack>
 7 #include<vector>
 8 using namespace std;
 9 
10 const int maxn=110;
11 vector<int>tp[maxn];//用来储存依赖关系
12 int b[maxn];//储存依赖度
13 queue<int>q,ans;//q队列用来存入度为零的元素(即没有元素比他小),,ans队列用来储存顺序
14 int n,m;
15 
16 int main()
17 {
18     int x,y;
19     while(scanf("%d%d",&n,&m)&&(n||m))
20     {
21         memset(b,0,sizeof(b));
22         for(int i=0;i<n;i++)  tp[i].clear();//初始化!!!
23         for(int i=0;i<m;i++)
24         {
25             scanf("%d%d",&x,&y);
26             tp[x].push_back(y);//记录依赖关系
27             b[y]++;//记录y元素依赖度(即比多少个元素大)
28         }
29         for(int i=1;i<=n;i++)
30             if(!b[i]) q.push(i);//将入度为零的元素压入队列
31         while(!q.empty())//开始剪边
32         {
33             int t=q.front();
34             ans.push(t);
35             q.pop();
36             for(int i=0;i<tp[t].size();i++)
37                 {
38                     b[tp[t][i]]--;//依赖度--;
39                     if(b[tp[t][i]]==0) //依赖度为零
40                         q.push(tp[t][i]);
41                 }
42         }
43         while(!ans.empty())
44         {
45             if(ans.size()>1) printf("%d ",ans.front());
46             else printf("%d\n",ans.front());
47             ans.pop();
48         }
49     }
50     return 0;
51 }
另一种

 

转载于:https://www.cnblogs.com/yijiull/p/6775436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值