题目链接:http://poj.org/problem?id=2492
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 39415 | Accepted: 12835 |
Description
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
Output
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 2e3+10; 19 20 int n, m; 21 int fa[MAXN], r[MAXN]; 22 23 int find(int x) 24 { 25 if(fa[x]==-1) return x; 26 int pre = find(fa[x]); 27 r[x] = (r[x]+r[fa[x]])%2; 28 return fa[x] = pre; 29 } 30 31 bool Union(int u, int v) 32 { 33 int fu = find(u); 34 int fv = find(v); 35 if(fu==fv) 36 return r[u]==r[v]; 37 38 fa[fu] = fv; 39 r[fu] = (-r[u]+1+r[v])%2; 40 return false; 41 } 42 43 int main() 44 { 45 int T; 46 scanf("%d", &T); 47 for(int kase = 1; kase<=T; kase++) 48 { 49 scanf("%d%d", &n, &m); 50 memset(fa, -1, sizeof(fa)); 51 memset(r, 0, sizeof(r)); 52 53 bool flag = true; 54 for(int i = 1; i<=m; i++) 55 { 56 int u, v; 57 scanf("%d%d", &u, &v); 58 if(Union(u, v)) 59 flag = false; 60 } 61 62 printf("Scenario #%d:\n", kase); 63 printf("%s\n\n", flag?"No suspicious bugs found!":"Suspicious bugs found!"); 64 } 65 }
代码二:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 2e3+10; 19 20 int n, m; 21 int fa[MAXN], r[MAXN]; 22 23 int find(int x) 24 { 25 if(fa[x]==-1) return x; 26 int pre = find(fa[x]); 27 r[x] ^= r[fa[x]]; 28 return fa[x] = pre; 29 } 30 31 bool Union(int u, int v) 32 { 33 int fu = find(u); 34 int fv = find(v); 35 if(fu==fv) 36 return r[u]==r[v]; 37 38 fa[fu] = fv; 39 r[fu] = r[u]^1^r[v]; 40 return false; 41 } 42 43 int main() 44 { 45 int T; 46 scanf("%d", &T); 47 for(int kase = 1; kase<=T; kase++) 48 { 49 scanf("%d%d", &n, &m); 50 memset(fa, -1, sizeof(fa)); 51 memset(r, 0, sizeof(r)); 52 53 bool flag = true; 54 for(int i = 1; i<=m; i++) 55 { 56 int u, v; 57 scanf("%d%d", &u, &v); 58 if(Union(u, v)) 59 flag = false; 60 } 61 62 printf("Scenario #%d:\n", kase); 63 printf("%s\n\n", flag?"No suspicious bugs found!":"Suspicious bugs found!"); 64 } 65 }