【裸的并查集】POJ 1611 The Suspects

http://poj.org/problem?id=1611

【Accepted】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include <string>
 6 using namespace std;
 7 const int maxn=3e4+3;
 8 int fa[maxn];
 9 int n,m;
10 int a[maxn];
11
12 void init()
13 {
14     for(int i=0;i<n;i++)
15     {
16         fa[i]=i;
17     }
18 }
19 int find(int x)
20 {
21     return x==fa[x]?x:fa[x]=find(fa[x]);
22 }
23
24 void mix(int x,int y)
25 {
26     int fx=find(x);
27     int fy=find(y);
28     if(fx!=fy)
29     {
30         fa[fx]=fy;
31     }
32 }
33
34 int main()
35 {
36     while(~scanf("%d%d",&n,&m))
37     {
38         if(n==0&&m==0)
39         {
40             break;
41         }
42         init();
43         for(int i=0;i<m;i++)
44         {
45             int k;
46             scanf("%d",&k);
47             if(k>=1)
48             {
49                 scanf("%d",&a[0]);
50                 for(int j=1;j<k;j++)
51                 {
52                     scanf("%d",&a[j]);
53                     mix(a[0],a[j]);
54                 }
55             }
56         }
57         int sum=0;
58         for(int i=0;i<n;i++)
59         {
60             if(find(i)==fa[0])
61             {
62                 sum++;
63             }
64         }
65         printf("%d\n",sum);
66     }
67     return 0;
68 }

【不太明白】

为啥find(i)==fa[0]能A,fa[i]==fa[0]就是WA。find(0)一定等于fa[0]?

时间: 2024-10-06 06:21:21

【裸的并查集】POJ 1611 The Suspects的相关文章

[并查集] POJ 1611 The Suspects

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

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 1611 The Suspects(特别无语的并查集)

很简单的一道题目,开始用的是并查集的分离集合森林做,不知道怎么的特别不稳定,太奇怪了,WA无数次,无奈之下改成一维数组了..sad AC #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define PI acos(-1,0) using namespa

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

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

poj 1611:The Suspects(并查集,经典题)

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

Catenyms+欧拉回路/欧拉路+并查集+POJ

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9617   Accepted: 2524 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

HDU 1102 Constructing Roads (裸的并查集)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13210    Accepted Submission(s): 4995 Problem Description There are N villages, which are numbered from 1 to N, and you should

POJ 1611 The Suspects (并查集+数组记录子孙个数 )

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

POJ 1611 The Suspects 并查集 Union Find

本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每一个节点使用find parent操作.由于最后状态的时候,节点的parent不一定是本集合的根节点. #include <stdio.h> const int MAX_N = 30001; struct SubSet { int p, rank; }sub[MAX_N]; int N, M; v

POJ 1611 The Suspects (并查集求数量)

Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In t