# POJ2492(A Bug's Life) && POJ1703(Find them, Catch them)带权(种类)并查集

169人阅读 评论(0)

A Bug's Life
 Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30559 Accepted: 9995

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

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!

Find them, Catch them
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35456 Accepted: 10883

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

Sample Input

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4


Sample Output

Not sure yet.
In different gangs.
In the same gang.

## POJ2492:

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

#define MAX 5005
int parent[MAX];	//记录父节点
int relations[MAX];	//relations[x]记录x和父节点之间的关系
//其中relations[x] = 0表示x和父节点关系为同性， relations[x] = 1表示x和父节点关系为异性

//集合查找
int findSet(int x)
{
int temp = parent[x];
if(temp == x)
return x;

parent[x] = findSet(temp);
relations[x] = (relations[x] + relations[temp]) % 2;	//根据老的父节点和新父节点关系，修改relations[x]值

return parent[x];
}

//合并集合
void unionSet(int root1, int root2)
{
int x = findSet(root1);
int y = findSet(root2);

if(x == y)
return ;

parent[x] = y;
relations[x] = ((relations[root1] - relations[root2] + 1) % 2);
return ;
}

//初始化数据
void init(void)
{
memset(relations, 0, sizeof(relations));
for(int i = 0; i < MAX; i++)
parent[i] = i;

return ;
}

int main()
{
int temp1, temp2;
int n, m;
int num;
int count = 1;

scanf("%d", &num);
while(num--)
{
scanf("%d%d", &n, &m);
init();
int flag = 1;
for(int i = 0; i < m; i++)
{
scanf("%d%d", &temp1, &temp2);
if(findSet(temp1) == findSet(temp2))
{
if(relations[temp1] != (relations[temp2] + 1) % 2)	//如果不满足异性关系，有矛盾
flag = 0;
}
else
{
unionSet(temp1, temp2);
}
}
if(flag)
{
printf("Scenario #%d:\nNo suspicious bugs found!\n\n", count++);
}
else
{
printf("Scenario #%d:\nSuspicious bugs found!\n\n", count++);
}
}

return 0;
}

## POJ1703:

#include<iostream>
#include<cstdio>
using namespace std;

const int maxn = 100010;
int father[maxn];
int offset[maxn]; //offset是a -> fa(a的根)

int pos1,pos2;

void init(int cnt)
{
int i;
for(i=1;i<=cnt;i++)
{
father[i] = i;
offset[i] = 0; //偏移量初始化
}
}

int find(int x)
{
int fx;
if(x != father[x])
{
fx = father[x]; //记录父结点
father[x] = find(father[x]);
offset[x] = (offset[x] + offset[fx]) % 2; //x -> fx(root),2组;
}
return father[x];
}

void Union(int a,int b)
{
int fx = find(a);
int fy = find(b);
if(fx != fy)
{
father[fx] = fy;
offset[fx] = (3 - offset[a] + offset[b]) % 2;
//offset[fx] = (1 +offset[b]-offset[a])%2;
}
}

int main()
{
int T;
int m,n;
char Q[2];
int num1,num2;
int fa,fb;
int i;
//freopen("1.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
cin>>m>>n;
init(m);
for(i=1;i<=n;i++)
{
scanf("%s%d%d",Q,&num1,&num2);
if(Q[0] == 'D')
{
Union(num1,num2);
}
else
{
if(m == 2)
{
printf("In different gangs.\n");
}
else
{
fa = find(num1);
fb = find(num2);
if(fa != fb)
{
printf("Not sure yet.\n");
continue;
}
if(offset[num1] != offset[num2])
{
printf("In different gangs.\n");
continue;
}
printf("In the same gang.\n");
}
}
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：63416次
• 积分：2357
• 等级：
• 排名：第16528名
• 原创：178篇
• 转载：15篇
• 译文：0篇
• 评论：6条
阅读排行
评论排行
最新评论