poj2492 (并查集,两个种类)
A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 44540 | Accepted: 14415 |
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!
Hint
Huge input,scanf is recommended.
分析:题目意思是给定n只虫子,不同性别的可以在一起,同性的不能在一起,
给你m对虫子,判断中间有没有同性在一起的。
难点在于如果当前两个虫子a,b还没有确定性别时,无法判断a=雌,b=雄和a=雄,b=雌哪个正确。
可以把第i个虫子分为两个点,即i跟i+N分别代表两种性别,
让a和b+N合并,b和a+N合并,最后判断a和a+N是否在同一集合内,b和b+N是否在同一集合内。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int N; int pre[10000]; void init() { for(int i=0;i<=2*N;i++) pre[i]=i; } int find(int x) { if(x!=pre[x]) pre[x]=find(pre[x]); return pre[x]; } void unit(int x,int y) { int rootx=find(x),rooty=find(y); if(rootx<rooty) pre[rooty]=pre[rootx]; else pre[rootx]=pre[rooty]; } bool judge(int x,int y) { int rootx=find(x),rooty=find(y); if(rootx==rooty) return false; return true; } int main() { int T,M,cas=0; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); init(); int flag=0; while(M--) { int x,y; scanf("%d%d",&x,&y); if(judge(x,y)||judge(x+N,y+N)) { unit(x,y+N);//异性合并 unit(x+N,y); } else flag=1; } if(!flag) printf("Scenario #%d:\nNo suspicious bugs found!\n",++cas); else printf("Scenario #%d:\nSuspicious bugs found!\n",++cas); printf("\n"); } return 0; }
作者:ACRykl —— O ever youthful,O ever weeping!
出处:http://www.cnblogs.com/ACRykl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。