poj 2492A Bug's Life(并查集)

 1 /*
 2 目大意:输入一个数t,表示测试组数。然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配。
 3 要求统计交配过程中是否出现冲突,即是否有两个同性的昆虫发生交配。
 4
 5 思路:并查集
 6    将同性的昆虫放入集合之中,如果输入的昆虫u, v同时出现在了集合中,那么 u,v就是同性的!发生了同性交配的可能!
 7 */
 8
 9 #include <string>
10 #include <cstdio>
11 #include <cstring>
12 #include <iostream>
13
14
15
16 using namespace std;
17 int n, m;
18 int f[2010];
19 int  mark[2010];//mark[i]表示 与 i 交配的昆虫的编号!
20
21 int getFather(int x){
22    return x==f[x] ? x : f[x]=getFather(f[x]);
23 }
24
25 void Union(int a, int b){
26     int fa=getFather(a), fb=getFather(b);
27     if(fa!=fb)
28        f[fa]=fb;
29 }
30
31 int main(){
32    int t, cnt=0;
33    scanf("%d", &t);
34    while(t--){
35
36           scanf("%d%d", &n, &m);
37           for(int i=1; i<=n; ++i)
38           f[i]=i;
39       memset(mark, 0, sizeof(mark));
40       int flag=1;
41       while(m--){
42              int u, v;
43          scanf("%d%d", &u, &v);
44          if(flag){
45             if(getFather(u) == getFather(v)){
46                flag=0;
47                            continue;
48              }
49                         if(!mark[u] && !mark[v]){
50                    mark[u]=v;
51                    mark[v]=u;
52                  }
53                 else if(!mark[u]){
54                    mark[u]=v;
55                    Union(u, mark[v]); //如果v配对了,u没有配对,那么u和mark[v]就是同性昆虫,放入集合之中
56             }
57                  else if(!mark[v]){
58                    mark[v]=u;
59                    Union(v,mark[u]);//,,,,,,
60                 }
61                else{
62                   Union(u, mark[v]);//如果之前u和v都已经配对,现在u和v进行配对, 那么u和mark[v]是同性, v和mark[u]是同性!
63               Union(v, mark[u]);
64                }
65          }
66       }
67       printf("Scenario #%d:\n",++cnt);
68         if (flag==1)
69             printf("No suspicious bugs found!\n");
70         else
71             printf("Suspicious bugs found!\n");
72         printf("\n");
73    }
74 }

poj 2492A Bug's Life(并查集)

时间: 2024-12-12 09:58:08

poj 2492A Bug's Life(并查集)的相关文章

POJ 2492 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

POJ 2492 A Bug&#39;s Life (并查集)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

A Bug&#39;s Life POJ - 2492 (带权并查集)

A Bug's Life POJ - 2492 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, indi

poj2492 A Bug&#39;s Life (并查集拓展)

C - A Bug's Life Crawling in process... Crawling failed Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: CSUST_11 (2013-04-14) System Crawler (2016-05-13) Description B

hdu1829 A Bug&#39;s Life(并查集)

开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,如果不冲突就进行合并,否则不需要继续合并. #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int MAX=2000; int pre[2*MAX+5]; bool mark; void init(int n){ int i; for(i=1

poj2492--A Bug&#39;s Life(并查集变形)

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

hdu 1829 A Bug&#39;s Life 并查集系列

1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 using namespace std; 7 8 #define MAXN 2222 9 int fa[MAXN]; 10 int rnk[MAXN]; //秩 表示某点与根的距离 11 int n,

POJ 1611 The Suspects(并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22296   Accepted: 10842 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #