zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",ans.front());
    47             ans.pop();
    48         }
    49     }
    50     return 0;
    51 }
    另一种
  • 相关阅读:
    【TCP/IP】【网络基础】网页访问流程
    【linux】【rpm】确定程序是否 rpm 安装
    【linux】【CPU】【x86】平台说明
    【linux】【进程】stand alone 与 super daemon 区别
    【内存】堆内存和栈内存
    【英语】【音标】元音字母 和 开音节 闭音节
    【php】【异步】php实现异步的几种方法
    【操作系统】多处理器系统的用户模式和特权模式
    张量漫谈(第三篇)
    张量漫谈(前两篇)
  • 原文地址:https://www.cnblogs.com/yijiull/p/6775436.html
Copyright © 2011-2022 走看看