poj-2492
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.
解题报告:
题意是让我们分析出是否有同性恋,那么我们可以求出每个点到根节点的距离,利用每个点到根节点的差值来判断是否同性,如果差值为0就说明是同性,如果差值为1就为异性。
#include<stdio.h>
#include<cstring>
using namespace std;
const int N=2010;
int p[N];
int d[N];
int find(int x)
{
if(x!=p[x])
{
int t=find(p[x]);
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main()
{
int t;
int k=0;
scanf("%d",&t);
while(t--)
{
bool flag=true;
memset(d,0,sizeof d);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
p[i]=i;
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(!flag)
continue;
int px=find(a),py=find(b);
if(px==py)
{
if((d[a]-d[b]-1)%2)
{
flag=false;
}
}
else
{
p[px]=py;
d[px]=d[b]-d[a]+1;
}
}
printf("Scenario #%d:\n",++k);
if(flag)
printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
printf("\n");
}
return 0;
}
题目2
POJ - 1182
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。
现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这N个动物所构成的食物链关系进行描述:
第一种说法是"1 X Y",表示X和Y是同类。
第二种说法是"2 X Y",表示X吃Y。
此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
Input
第一行是两个整数N和K,以一个空格分隔。
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。
Output
只有一个整数,表示假话的数目。
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
#include<iostream>
#include<stdio.h>
using namespace std;
const int N=50010;
int p[N],d[N];
int find(int x)
{
if(x!=p[x])
{
int t=find(p[x]);
// cout<<d[x]<<endl;
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main()
{
int res=0;
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
p[i]=i;
while(k--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(b>n||c>n) {
res++;
continue;
}
int px=find(b),py=find(c);
if(a==1)
{
if(px==py&&(d[b]-d[c])%3) res++;
else if(px!=py)
{
p[px]=py;
d[px]=d[c]-d[b];
}
}
else
{
if(px==py&&(d[b]-d[c]-1)%3) res++;
else if(px!=py)
{
p[px]=py;
d[px]=d[c]-d[b]+1;
}
}
}
cout<<res<<endl;
return 0;
}
poj 1703
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.
#include<stdio.h>
#include<cstring>
using namespace std;
const int N=100010;
int d[N],p[N];
int find(int x)
{
if(x!=p[x])
{
int t=find(p[x]);
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(d,0,sizeof d);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
p[i]=i;
while(m--)
{
char b[2];
int x,y;
scanf("%s",b);
scanf("%d%d",&x,&y);
// printf("%c",b);
if(b[0]=='A')
{
int px=find(x),py=find(y);
if(px==py)
{
if(((d[x]-d[y])%2+2)%2==1)
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
else
{
printf("Not sure yet.\n");
}
}
else
{
int px=find(x),py=find(y);
p[px]=py;
d[px]=d[y]-d[x]+1;
}
}
//printf("\n");
}
return 0;
}