我现在做的是第四专题编号为1019的试题,具体内容没如下所示:
Problem S
Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 19 Accepted Submission(s) : 7
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
简单题意:
给定一系列数对,例如a和b,表示a和bb不是同一种性别,然后不断的给出这样的数对,问有没有性别不对的情况。
解题思路:
编写代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 2000;
int pre[MAX+5];
int offset[MAX+5];
bool mark;
void init(int n){
int i;
for(i=1;i<=n;++i){
pre[i] = i;
offset[i] = 0;
}
mark = true;
}
int root(int x){
int px;
if(x!=pre[x]){
px = pre[x];
pre[x] = root(pre[x]);
offset[x] = (offset[px] + offset[x])%2;
}
return pre[x];
}
void merge(int x,int y){
int fx = root(x);
int fy = root(y);
if(fx!=fy){
pre[fx] = fy;
offset[fx] = (1 + offset[y] - offset[x])%2;
}
root(x);
}
int main(){
int t,i,n,m,x,y,k;
scanf("%d",&t);
for(i=1;i<=t;++i){
scanf("%d %d",&n,&m);
init(n);
for(k=1;k<=m;++k){
scanf("%d %d",&x,&y);
if(mark){
merge(x,y);
}
if(offset[x]==offset[y]){
mark = false;
}
}
printf("Scenario #%d:\n",i);
if(mark){
printf("No suspicious bugs found!\n");
}else{
printf("Suspicious bugs found!\n");
}
printf("\n");
}
return 0;
}