POJ 2492 A Bug's Life
来源:互联网 发布:金山手机数据恢复 编辑:程序博客网 时间:2024/06/06 00:44
分析:给你多只虫子的关系,判断有没有gay,一开始初始化他们都没有对象relative[i]=0;如果a,b都没有对象,那么a的对象是b,b的对象是a;如果a有,b没有,b的对象就是a,把b加入到a的对象的集合中;b有,a没有同理;如果a,b都有对象,就把他们加入到各自的对象集中,一只虫子的第一个对象为老大,后来加进来的都要归他管。
# include <stdio.h> int flag,root[2005],relative[2005]; int find(int x) { if(x==root[x]) return x; root[x]=find(root[x]); return root[x]; } void Union(int x,int y) { int fx=find(x),fy=find(y); root[fx]=fy; } int main() { int a,b,T,n,m,i,j,t; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { root[i]=i; relative[i]=0; } flag=0; while(m--) { scanf("%d%d",&a,&b); if(flag==1) continue; if(find(a)==find(b)) flag=1; if(relative[a]==0&&relative[b]==0) { relative[a]=b; relative[b]=a; } else if(relative[a]==0) { relative[a]=b; Union(a,relative[b]); } else if(relative[b]==0) { relative[b]=a; Union(b,relative[a]); } else { Union(a,relative[b]); Union(b,relative[a]); } } if(flag==1) printf("Scenario #%d:\nSuspicious bugs found!\n\n",t); else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",t); } return 0; }
0 0
- poj 2492 A Bug's Life
- POJ-2492 A Bug's Life
- Poj 2492 A Bug's Life
- poj 2492 A Bug's Life
- POJ 2492 - A Bug's Life
- POJ 2492 A Bug's Life
- POJ-2492-A Bug's Life
- poj 2492 A Bug's Life
- poj 2492 A Bug's Life
- poj 2492 A Bug's Life
- POJ 2492 A Bug's Life
- poj 2492 A Bug's Life
- POJ-2492-A Bug's Life
- poj 2492 A Bug's Life
- poj 2492 A Bug's Life
- POJ 2492 A Bug's Life
- POJ 2492 A Bug's Life
- POJ 2492 A Bug's Life
- ES6详解一:复习ES5 新增内容
- LA3882 约瑟夫问题变形
- 第1周编程题-表示分数类的Fraction
- android小错误:Failure retrieving text 0x7f050001 in package
- AndroidAnnotations框架@Ebean,@RootContext,@Background,@UiThread,@AfterInject,@AfterTextChange标签的使用方法
- POJ 2492 A Bug's Life
- Java多线程-并发协作(生产者消费者模型)
- SpringMVC Controller 介绍
- 【操作系统】如何用程序确定当前系统的存储模式
- 欢迎给我发邮件
- WINCE 程序自启动设置总结
- 【二】关于界面设计和监听实现
- pandas重写Q5
- IOS编程的一些基本概念