http://poj.org/problem?id=2492
http://blog.csdn.net/u014004096/article/details/39009173
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!
http://www.java3z.com/cwbwebhome/article/article18/report60.html?id=4715
将所有虫子分到两个集合中,公的一个集合,母的一个集合,读入一对信息时,如果它们是属于同一集合,则发生同性恋.
题意:给你 N 条虫 , 和它们间的 M 条关系。
每条关系 X Y 表示 X 和 Y 不同类 ,在检验每条关系的时候,一旦发现了矛盾
也就是当前的这组 X 和 Y 根据前面的关系已经判定是同类的,但是这里给出的关系又是应该不同类,
则输出 Suspicious bugs found!
如果从未发生矛盾,则输出No
suspicious bugs found!
算法:并查集的简单应用,判断是否是同类。
注意:输出格式【换行,和属于第几组数据】
思路:用数组 r[]记录与父亲节点的关系,0 则同类 1 则异类
用并查集检验是否有关系:如果有关系,r[]同则矛盾。
如果没有关系,则合并两棵子树。
- const int maxn = 2000+10;
- int p[maxn]; //记录父节点
- int r[maxn]; //记录与父节点关系, 0 同类, 1异类
- int find(int x)
- {
- if(x == p[x]) return x;
- int t = p[x];
- p[x] = find(p[x]);
- r[x] = (r[x]+r[t])%2; //每次回溯更新一次父节点,相应更新关系
- return p[x];
- }
- void Union(int x, int y)
- {
- int fx = find(x);
- int fy = find(y);
- p[fx] = fy; //任意
- r[fx] = (r[x]+1+r[y])%2; //r[]没有方向
- }
- void set(int n)
- {
- for(int i = 1; i <= n; i++)
- {
- p[i] = i;
- r[i] = 0;
- }
- }
- int main()
- {
- int T;
- scanf("%d", &T);
- for(int i = 1; i <= T; i++)
- {
- int n, m;
- scanf("%d%d", &n, &m);
- set(n);
- int x, y;
- bool flag = true;
- while(m--)
- {
- scanf("%d%d", &x, &y); //本应不同类
- if(find(x) == find(y))
- {
- if(r[x] == r[y]) //如果同类
- {
- flag = false;
- continue;
- }
- }
- else Union(x, y);
- }
- printf("Scenario #%d:\n", i);
- if(flag) printf("No suspicious bugs found!\n");
- else printf("Suspicious bugs found!\n");
- printf("\n");
- }
- return 0;
- }
http://blog.csdn.net/u014004096/article/details/39009173
- int yix[2014],f[2014];
- void init(int n)
- {
- for(int i=1; i<=n; i++)
- {
- f[i]=i;
- yix[i]=0;
- }
- }
- int find(int x)
- {
- return f[x]==x?x:find(f[x]);
- }
- void unity(int a,int b)
- {//合并同性的bug
- int x=find(a);
- int y=find(b);
- if(x!=y)
- f[x]=y;
- }
- int main()
- {
- int T;
- cin>>T;
- for(int i=1; i<=T; i++)
- {
- bool tx_istrue=false;
- int n,m,a,b;
- cin>>n>>m;
- init(n);
- while(m--)
- {
- //如果yix[num]不为0的话表示num肯定与编号为yix[num]的异性bug交配过!
- cin>>a>>b;
- if(!yix[a]&&!yix[b])
- {
- yix[a]=b;//如果这两只bug前面都没有出现过,
- yix[b]=a;//则标记数组分别标记对方为异性!
- }
- else if(!yix[a]&&yix[b])
- {
- yix[a]=b;
- unity(a,yix[b]);//编号为a的bug肯定与编号为yix[b]的bug同性!
- }
- else if(yix[a]&&!yix[b])
- {
- yix[b]=a;
- unity(b,yix[a]);//编号为b的bug肯定与编号为yix[a]的bug同性!
- }
- else
- {//此处即我上面所说的判断,在这表明a和b之前都分别与编号为yix[a]和yix[b]的异性bug交配过!
- unity(yix[a],b);
- unity(yix[b],a);
- }
- if(find(a)==find(b))
- tx_istrue=true;//每输入一次就就行一次判断,判断a和b是否是同性!
- }
- printf("Scenario #%d:\n",i);
- if(tx_istrue)
- printf("Suspicious bugs found!\n");
- else
- printf("No suspicious bugs found!\n");
- printf("\n");
- }
- return 0;
- }