POJ 2492/hdu 1829 A Bug's Life【带权并查集】
2016-04-12 21:52
519 查看
A Bug's Life
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy
to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each
interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual
behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
Sample Output
Hint
Huge input,scanf is recommended.
以前做这个题的时候用的是二分图染色法,今天试一试用并查集做。
题目大意:
有n只虫子,m个关系,关系表示x,y是异性恋爱关系,如果有同性恋,输出 found,否则输出no found、
思路:尽管给出的m个关系是指代两个虫子不属于一个性别,但是我们依旧能用一个并查集来做这个问题,将有关系的两个虫子放在一个并查集中,并且对其设置边权,当
a-b之间有直接关系表示这两个虫子不属于一个性别,那么对其设置边权为1.辣么我们有这样的结论:如果a到b的边权值为偶数的时候表示两者同性别,如果a到b的边权值为奇数的时候表示不同性别。
在连接的时候有这样的关系:
1+sumb==suma+x
x=1+sumb-suma。
这个时候我们就能够通过已知权值求未知权值了,思路已经很清晰,最后上AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int f[2500];
int sum[2500];
int find(int x)
{
if(x!=f[x])
{
int pre=f[x];//pre是x的一个父节点。
f[x]=find(f[x]);//递归找祖先。
sum[x]+=sum[pre];//路径压缩部分。
}
return f[x];
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
int n,m;
int flag=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
f[i]=i;
sum[i]=0;
}
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
sum[yy]=1-sum[y]+sum[x];
f[yy]=xx;
}
else
{
if((sum[y]-sum[x])%2==0)//如果距离为偶数
{
flag=1;//标记上有同性恋
}
}
}
printf("Scenario #%d:\n",++kase);
if(flag==1)
{
printf("Suspicious bugs found!\n");
}
else
{
printf("No suspicious bugs found!\n");
}
printf("\n");
}
}
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 33135 | Accepted: 10859 |
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy
to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each
interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual
behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
Huge input,scanf is recommended.
以前做这个题的时候用的是二分图染色法,今天试一试用并查集做。
题目大意:
有n只虫子,m个关系,关系表示x,y是异性恋爱关系,如果有同性恋,输出 found,否则输出no found、
思路:尽管给出的m个关系是指代两个虫子不属于一个性别,但是我们依旧能用一个并查集来做这个问题,将有关系的两个虫子放在一个并查集中,并且对其设置边权,当
a-b之间有直接关系表示这两个虫子不属于一个性别,那么对其设置边权为1.辣么我们有这样的结论:如果a到b的边权值为偶数的时候表示两者同性别,如果a到b的边权值为奇数的时候表示不同性别。
在连接的时候有这样的关系:
1+sumb==suma+x
x=1+sumb-suma。
这个时候我们就能够通过已知权值求未知权值了,思路已经很清晰,最后上AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int f[2500];
int sum[2500];
int find(int x)
{
if(x!=f[x])
{
int pre=f[x];//pre是x的一个父节点。
f[x]=find(f[x]);//递归找祖先。
sum[x]+=sum[pre];//路径压缩部分。
}
return f[x];
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
int n,m;
int flag=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
f[i]=i;
sum[i]=0;
}
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
sum[yy]=1-sum[y]+sum[x];
f[yy]=xx;
}
else
{
if((sum[y]-sum[x])%2==0)//如果距离为偶数
{
flag=1;//标记上有同性恋
}
}
}
printf("Scenario #%d:\n",++kase);
if(flag==1)
{
printf("Suspicious bugs found!\n");
}
else
{
printf("No suspicious bugs found!\n");
}
printf("\n");
}
}
相关文章推荐
- HDU 1829 A Bug's Life (并查集)
- POJ 2492-A Bug's Life(带权并查集)
- css 2D变形
- css 2D变形
- 在浏览器中显示TXT 文本中的换行(html/txt)
- 20160412jsp学习笔记简单了解jsp
- 剑指offer系列之49:数组中重复的数字
- 剑指offer系列之48:把字符串转换成整数
- 有向图中Dijstra最短路径算法的邻接表实现
- 剑指offer系列之47:不用加减乘除做加法
- 【数据存储】——JSON与XML的对比
- javascript copy拷贝对象
- java解析XML Node与Element的区别
- node学习之express(1)
- 剑指offer系列之45:圆圈中最后剩下的数字
- 剑指Offer(java答案)
- ubuntu 14.04 ns2.35 ***buffer overflow detected **: ns terminated解决办法
- jsp用到的标签
- 【数据存储】——JSON的解析和生成
- jquery的checkbox,radio,select等方法总结