Graph Theory NO.3 HDU_1325_Is It A Tree_并查集
来源:互联网 发布:淘宝宝贝属性怎么设置 编辑:程序博客网 时间:2024/06/05 13:27
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define M 100009int k=1;int father[M];int d[M];//入度int mark[M];void Init(int n)//初始化{ for(int i=1;i<=n;i++) { father[i]=i; }}int Find(int n)//查找根{ if(n!=father[n]) { father[n]=Find(father[n]); } return father[n];}int Union(int n,int m){ if(n==m) return 0; else { father[n]=m; } return 0;}int main(){ int a,b; memset(d,0,sizeof(d)); int num=0; int flag=0; Init(M); int count=0; memset(mark,0,sizeof(mark)); while(~scanf("%d%d",&a,&b)) { if(a<0&&b<0) break; if(a!=0&&b!=0) { mark[num++]=a; mark[num++]=b; if(father[a]==father[b]) { flag=1; //printf("*1\n"); } Union(Find(a),Find(b)); d[b]++; } else { for(int i=0;i<num;i++) { if(d[mark[i]]>1) { flag=1; //printf("*2\n"); } if(father[mark[i]]==mark[i]) { count++; //printf("*3\n"); } } if(count>1) { flag=1; //printf("*4\n"); } if(flag==0) { printf("Case %d is a tree.\n",k++); } else { printf("Case %d is not a tree.\n",k++); } memset(d,0,sizeof(d)); memset(mark,0,sizeof(mark)); num=0; flag=0; Init(M); count=0; } } return 0;}/*6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0*/
阅读全文
0 0
- Graph Theory NO.3 HDU_1325_Is It A Tree_并查集
- Graph Theory NO.1 HDU_1213_How Many Tables_并查集
- Graph Theory NO.4 HDU_1856_More is better_并查集
- Graph Theory NO.2 HDU_1272_小希的迷宫_并查集
- Graph Theory NO.6 HDU_1232_畅通工程_并查集
- hdu_1325_Is It A Tree?(DFS)
- 【并查集】Is it a tree?
- 【并查集】Is it A Tree?
- Is It A Tree?(并查集)
- Is It A Tree? (并查集)
- Is It A Tree? 并查集
- Is It A Tree?(并查集)
- POJ 1308-Is It A Tree 并查集
- HDU 1325 Is It A Tree?(并查集)
- Is It A Tree?(并查集)
- POJ1308--并查集--Is It A Tree?
- POJ 1308 Is It A Tree? (并查集)
- Is It A Tree?--POJ1308--并查集
- POJ1125股票经纪人dijkstra/Floyd动态规划
- free()
- Codeforces 869C Round #439 Div2 C:排列组合计数
- 51Nod
- C++面试集锦( 面试被问到的问题 )
- Graph Theory NO.3 HDU_1325_Is It A Tree_并查集
- JAVA新手牛刀小试之提取相同字符组成的子串
- 前端代码重构
- 使用MyEclipse开发web工程常见问题
- JS之匿名函数
- 模板:Floyed
- Vue的axios跨域问题
- WPS参考文献自动生成方法
- PAT 甲级 1107. Social Clusters (30)