并查集(2)
2015-08-10 10:47
267 查看
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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[2005],y[2005];
void init(int n)
{
for(int i=0;i<n;i++)
f[i]=i;
}
int ff(int x)
{
if(f[x]!=x)
f[x]=ff(f[x]);
return f[x];
}
void join(int a,int b)
{
f[ff(a)]=f[ff(b)];
}
int main()
{
int T,n,m,cas=1;
scanf("%d",&T);
while(T--)
{
int flag=0;
cin>>n>>m;
init(n);
memset(y,0,sizeof(y));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(!(y[a]+y[b]))
{
y[a]=b;
y[b]=a;
}
else if(y[a]&&!y[b])
{
y[b]=a;
join(y[a],b);
}
else if(!y[a]&&y[b])
{
y[a]=b;
join(y[b],a);
}
else
{
join(y[a],b);
join(y[b],a);
}
if(ff(a)==ff(b))
flag=1;
}
printf("Scenario #%d:\n",cas++);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
if(T)
printf("\n");
}
return 0;
}
方法二:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 2005;
bool flag;
int fa, fb, n;
int father[MAX], rela[MAX];
int find(int x)
{
if(father[x] == -1)
return x;
else
{
int tmp = father[x];
father[x] = find(father[x]);
rela[x] = (rela[x]+rela[tmp])%2;
return father[x];
}
}
int main()
{
int T, m, a, b;
scanf("%d", &T);
while(T--)
{
flag = true;
scanf("%d%d", &n, &m);
memset(father, -1, (n+1)*sizeof(int));
memset(rela, 0, (n+1)*sizeof(int));
while(m--)
{
scanf("%d%d", &a, &b);
if(!flag)
continue;
fa = find(a);
fb = find(b);
if(fa != fb)
{
father[fa] = fb;
rela[fa] = (rela[a] + rela[b] + 1)%2;
}
else
if(rela[a] == rela[b])
flag = false;
}
printf("Scenario #%d:\n", k);
if(flag)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
return 0;
}
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!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[2005],y[2005];
void init(int n)
{
for(int i=0;i<n;i++)
f[i]=i;
}
int ff(int x)
{
if(f[x]!=x)
f[x]=ff(f[x]);
return f[x];
}
void join(int a,int b)
{
f[ff(a)]=f[ff(b)];
}
int main()
{
int T,n,m,cas=1;
scanf("%d",&T);
while(T--)
{
int flag=0;
cin>>n>>m;
init(n);
memset(y,0,sizeof(y));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(!(y[a]+y[b]))
{
y[a]=b;
y[b]=a;
}
else if(y[a]&&!y[b])
{
y[b]=a;
join(y[a],b);
}
else if(!y[a]&&y[b])
{
y[a]=b;
join(y[b],a);
}
else
{
join(y[a],b);
join(y[b],a);
}
if(ff(a)==ff(b))
flag=1;
}
printf("Scenario #%d:\n",cas++);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
if(T)
printf("\n");
}
return 0;
}
方法二:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 2005;
bool flag;
int fa, fb, n;
int father[MAX], rela[MAX];
int find(int x)
{
if(father[x] == -1)
return x;
else
{
int tmp = father[x];
father[x] = find(father[x]);
rela[x] = (rela[x]+rela[tmp])%2;
return father[x];
}
}
int main()
{
int T, m, a, b;
scanf("%d", &T);
while(T--)
{
flag = true;
scanf("%d%d", &n, &m);
memset(father, -1, (n+1)*sizeof(int));
memset(rela, 0, (n+1)*sizeof(int));
while(m--)
{
scanf("%d%d", &a, &b);
if(!flag)
continue;
fa = find(a);
fb = find(b);
if(fa != fb)
{
father[fa] = fb;
rela[fa] = (rela[a] + rela[b] + 1)%2;
}
else
if(rela[a] == rela[b])
flag = false;
}
printf("Scenario #%d:\n", k);
if(flag)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
return 0;
}
相关文章推荐
- 【原创】如何由结构体成员的地址逆算出结构体首地址
- Android高效加载大图、多图解决方案,有效避免程序OOM
- 根据数据,显示不同的数据颜色
- POJ 1159:Palindrome 最长公共子序列
- cocos2dx xcoder 上传APP审核碰到的各种问题整理
- 优化tableview的卡顿现象
- 可永久免费使用的SCADA系统
- 性能优化之Hibernate缓存讲解、应用和调优
- POJ 1159:Palindrome 最长公共子序列
- ML2 – Address Population
- 编程技术面试的五大要点
- Migration from classic replication to GTID replication(Online)
- javascript中caller和callee详解
- opencv 环境搭配
- 晒一下MAC下终端颜色配置
- 字符串比较 脚本练习
- HDU 5295 Unstable
- 51单片机之数码管
- 4.3 给圣诞老人的信息--Toast对象的使用
- javascript 延长作用域链 与没有块级作用域