【BestCoder Round #25】A(拓扑排序,找环)
来源:互联网 发布:python pack 编辑:程序博客网 时间:2024/04/27 13:32
题意不难看出是一道简单拓扑题目,自己对于拓扑一直没能掌握。这边就不写思路了,直接上代码了。
我的代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;bool DIS[110][110];int r[110];bool vis[110];int main(){int n, m;while (cin >> n >> m){int a, b;memset(r, 0, sizeof(r));memset(DIS, 0, sizeof(DIS));memset(vis, 0, sizeof(vis));for (int i = 0; i < m; i++){scanf("%d%d", &b, &a);if (DIS[a][b] == 0){DIS[a][b] = 1;r[b]++;}}if (n == 1){cout << "NO" << endl;continue;}int counts = 0;while (counts < n){int mid=-1;int i;for (i = 1; i <=n;i++)if (r[i] == 0&&!vis[i]){mid = i;vis[i] = 1;break;}if (mid==-1)break;for (int i = 1; i <= n;i++)if (DIS[mid][i])r[i]--;counts++;}if (counts == n)cout << "YES" << endl;elsecout << "NO" << endl;}}
别人的代码(比较规范的拓扑):
#include<iostream>#include<cstdio>#include<vector>#include<cstring>#include<queue>using namespace std;int main(){int r[110];int n, m;while (cin >> n >> m){int a, b;vector<int>g[110];memset(r, 0, sizeof(r));for (int i = 0; i < m; i++){scanf("%d%d", &a, &b);g[b].push_back(a);r[a]++;}queue<int> q;for (int i = 1; i <= n;i++)if (r[i] == 0)q.push(i);int counts = 0;while (!q.empty()){int k = q.front();q.pop();for (int i = 0; i < g[k].size(); i++){int v = g[k][i];r[v]--;if (r[v] == 0)q.push(v);}counts++;}cout << (counts == n ? "YES":"NO")<< endl;}}
0 0
- 【BestCoder Round #25】A(拓扑排序,找环)
- BestCoder Round #25 A,B
- hdu4857 & BestCoder Round #1 逃生(拓扑逆排序+优先队列)
- hdu 4857/BestCoder Round#1 1001(拓扑排序+逆向建图)
- Missing number (BestCoder Round #28A )
- BestCoder Round #41 -- (A,B)
- BestCoder Round #70(A)数学
- BestCoder Round #77 (div.2)(A)
- BestCoder Round #80(A)模拟
- BestCoder Round #19 A
- BestCoder Round #20 A
- BestCoder Round #21 A
- BestCoder Round #22 A
- BestCoder Round #23 A
- BestCoder Round #80A
- BestCoder Round #3 A,B
- hdu5804(BestCoder Round #86 A)
- BestCoder Round #92 A+B
- 观察者模式(java)浅析
- Leetcode: Binary Search Tree Iterator
- SQL查询操作处理流程
- 分页居中样式
- HDU 5154 Harry and Magical Computer (有向图判是否有环)
- 【BestCoder Round #25】A(拓扑排序,找环)
- linux共享内存之mmap
- Storeboard中Pan手势对ImageView不起作用解决办法
- Android DrawerLayout抽屉效果的导航菜单实现
- ios TextField 不被键盘遮住
- 常用分页代码居中
- 软件工程第三次大作业
- 为什么写技术博客
- linux 字节截取命令之cut