并查集 HDU1272
2015-08-10 18:27
387 查看
#include <iostream> #include <map> #include <cstring> #include <cstdio> using namespace std; int father[100010]; int num[100010]; int main() { int a,b; while(true) { int flag=1; int time=1; for(int i=1;i<=100000;i++) father[i]=i; for(int i=1;i<=100000;i++) num[i]=1; while(cin>>a>>b) { if(a==0&&b==0) break; if(a==-1&&b==-1) goto F1; time++; int fa=a; int fb=b; while(fa!=father[fa]) { fa=father[fa]; } while(fb!=father[fb]) { fb=father[fb]; } int t1=a; while(t1!=fa) { int tmp=father[t1]; father[t1]=fa; t1=tmp; } int t2=b; while(t2!=fb) { int tmp=father[t2]; father[t2]=fb; t2=tmp; } if(fa==fb) flag=0; else { father[fb]=fa; num[fa]+=num[fb]; num[fb]=0; } } if(flag) { flag=0; for(int i=1;i<=100000;i++) { if(num[i]==time) { flag=1; } } } if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } F1: return 0; }
View Code
相关文章推荐
- nsstring ==
- 负载均衡
- Linux系统下jailkit的安装与使用示例
- 程序如何排除
- 【黑马程序员】@property的参数
- (大数据工程师学习路径)第二步 Vim编辑器----Vim快速入门
- Java进阶05 多线程
- opencv2 cv::mat resize图像缩放代码
- 如何配置Spket环境的ExtJS支持
- HDU 1242 Rescue(BFS+优先队列)
- 大宁摇一摇
- 有100个节点的AVL树最大深度是多少?
- VFL示例
- HDU 1069 Monkey and Banana
- 线段树模版(转)
- POJ 3468 A Simple Problem with Integers (线段树,成段更新,区间求和)
- 关于jqGrid动态改变列的解决方案
- php5.3以上服务器单独安装php不安装mysql
- java 利用org.apache.poi.hssf包编写excel导出文件
- python 常用string函数