A Bug‘s Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28703 | Accepted: 9350 |
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.
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.
Source
TUD Programming Contest 2005, Darmstadt, Germany
题目大意:给出n条虫子。m个配对(男<->女)的情况。问数据有没有错误(出现同性配对)?
也就是说每一个给出的ab在不同的阵营,假设查询出现了在同一阵营就出现了错误。
c数组记录属于某一个阵营。d数组记录它所属的阵营的敌对阵营。
对每一组数据出现后。更新两个数组,注意:
假设a属于x,b属于y,那么应该讲d[y]归并到x中,y归并到d[x]中
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int c[2100] , d[2100] ; int find1(int a) { if( c[a] != a ) { c[a] = find1(c[a]) ; d[a] = d[ c[a] ] ; } return c[a] ; } int main() { int t , tt , i , j , n , m , a , b , flag ; scanf("%d", &t); for(tt = 1 ; tt <= t ; tt++) { scanf("%d %d", &n, &m); for(i = 1 ; i <= n ; i++) c[i] = i ; memset(d,-1,sizeof(d)); flag = 0 ; while(m--) { scanf("%d %d", &a, &b); if( flag ) continue ; int x , y ; x = find1(a) ; y = find1(b) ; if(x == y) flag = 1 ; else if( d[x] == -1 && d[y] == -1 ) { d[x] = y ; d[y] = x ; } else if( d[x] != -1 ) { c[y] = d[x] ; if( d[y] != -1 ) { int xx = find1(d[y]); c[xx] = x ; d[xx] = y ; } d[y] = x ; } else { c[x] = d[y] ; if( d[x] != -1 ) { int yy = find1(d[x]); c[yy] = y ; d[yy] = x ; } d[x] = y ; } } printf("Scenario #%d:\n", tt); if( flag ) printf("Suspicious bugs found!\n\n"); else printf("No suspicious bugs found!\n\n"); } return 0; }
poj2492--A Bug's Life(并查集变形)