真尴尬1180
来源:互联网 发布:兴安得力计价软件 编辑:程序博客网 时间:2024/04/30 03:23
结束的标志是两个小于0的数……
被套路了……..
#include <set>#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 10005;const int maxm = 10005;struct Edge{ int from,to,next;};Edge e[maxm];int head[maxn],edgeNum = 0,uset[maxn],du[maxn];void addEdge(int from,int to){ e[edgeNum].from = from; e[edgeNum].to = to; e[edgeNum].next = head[from]; head[from] = edgeNum; edgeNum++;}int Find(int x){ if(x == uset[x]) return x; else return uset[x] = Find(uset[x]);}int bfs(int rt){ queue<int> q; q.push(rt); int num = 1; while(!q.empty()) { int fa = q.front(); q.pop(); for(int j = head[fa]; j != 0; j = e[j].next) { q.push(e[j].to); num++; } } return num;}int main(){ //freopen("input.txt","r",stdin); int from,to,cas = 1; while(scanf("%d%d" ,&from,&to) != EOF) { if(from <= 0 || to <= 0) break; if(from == 0 && to == 0) { printf("Case %d is a tree.\n",cas++); continue; } memset(head,0,sizeof(head)); memset(du,0,sizeof(du)); edgeNum = 1; set<int> myset; myset.insert(from); myset.insert(to); addEdge(from,to); du[to]++; while(scanf("%d%d" ,&from,&to)) { if(from == 0 && to == 0) break; myset.insert(from); myset.insert(to); addEdge(from,to); du[to]++; } for(int i = 0; i < maxn; i++) uset[i] = i; bool OK = true; //认为不存在重边,环 for(int i = 1; i < edgeNum; i++) { int x = Find(e[i].from); int y = Find(e[i].to); if(x != y) uset[x] = y; else { OK = false; break; } } if(!OK) //存在重边,环 { printf("Case %d is not a tree.\n",cas++); continue; } int num = 0; //连通分量个数 for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr) { int x = *itr; if(uset[x] == x) num++; if(num >= 2) break; } if(num != 1) { printf("Case %d is not a tree.\n",cas++); continue; } else //只有一个连通分量 { int rt = -1; for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr) { int x = *itr; if(du[x] == 0) { rt = x; break; } } if(rt == -1) //没能找到根 { printf("Case %d is not a tree.\n",cas++); continue; } else { int cnt = bfs(rt); if(cnt != myset.size()) { printf("Case %d is not a tree.\n",cas++); } else { printf("Case %d is a tree.\n",cas++); } } } } return 0;}
0 0
- 真尴尬1180
- 真尴尬
- 尴尬
- 尴尬~
- 尴尬
- 根本不受我控制的浮动(一定要贴原创真的很尴尬)
- 尴尬相遇
- 尴尬境地!
- 打了2天的SteelSeries 高校精英赛,结束的比较尴尬,都说kof容易出事,真的出事了...丢人丢大了
- 尴尬的年龄,尴尬的位置。
- c++的尴尬
- 高学历的尴尬
- 生于七十年代的尴尬
- 七十年代的尴尬
- 年轻女郎的尴尬
- 丰满mm的尴尬
- 吴宗宪经典尴尬
- 四大经典尴尬
- 用c++和opencv写的电脑版flappy bird过关程序
- Google gson+async-http初体验
- 传入空的数据进行解析,发生crash
- linux 64位编译32位protobuf
- 在PHP中使用Redis
- 真尴尬1180
- docker-compose的pkg_resources.DistributionNotFound错误对应
- prototype、js中的三种方法、call
- Struts学习笔记:request,session,application的创建
- Linux 下使用静态google protocl buffer
- 打雷脚本
- Android开发——Snackbar使用详解
- Java2
- iframe 去除边框和自适应高度