poj 2492
Posted flyer_duck
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2492相关的知识,希望对你有一定的参考价值。
A Bug‘s Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 41952 | Accepted: 13619 |
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.
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!
感觉像套模板似的qwq。
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int maxn=2006; 5 int fa[maxn],val[maxn]; 6 7 void init() 8 { 9 for(int i=0;i<maxn;i++){ 10 fa[i]=i; 11 val[i]=0; 12 } 13 } 14 15 int find_fa(int x) 16 { 17 if(fa[x]==x) return x; 18 int tmp=fa[x]; 19 fa[x]=find_fa(tmp); 20 val[x]=val[x]^val[tmp]; 21 return fa[x]; 22 } 23 24 int main() 25 { 26 int T; 27 scanf("%d",&T); 28 for(int kkk=1;kkk<=T;kkk++){ 29 int n,m; 30 scanf("%d%d",&n,&m); 31 init(); 32 int ans=0; 33 for(int i=1;i<=m;i++){ 34 int x,y; 35 scanf("%d%d",&x,&y); 36 int opx,opy,tmp=1; 37 opx=find_fa(x); 38 opy=find_fa(y); 39 if(opx!=opy){ 40 fa[opy]=opx; 41 val[opy]=val[x]^val[y]^tmp; 42 } 43 if(opx==opy&&val[x]^val[y]!=tmp){ 44 ans=1; 45 } 46 } 47 printf("Scenario #%d:\n",kkk); 48 if(ans==1) printf("Suspicious bugs found!\n\n"); 49 else printf("No suspicious bugs found!\n\n"); 50 } 51 return 0; 52 }
以上是关于poj 2492的主要内容,如果未能解决你的问题,请参考以下文章