kuangbin_UnionFindB (POJ 1611)

过程是模板 merge完后扫一下几个跟0同祖先节点就是答案了

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
const int MAX=30005;
int father[MAX];
int findfather(int x)
{
    while(x!=father[x])
        x=father[x];
    return x;
}
void merge(int x,int y)
{
    x=findfather(x);
    y=findfather(y);
    if(x!=y) father[x]=y;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m),n||m)
    {
        int ans=0;
        for(int i=0;i<n;i++)
            father[i]=i;
        while(m--)
        {
            int k,fir,tmp;
            scanf("%d%d",&k,&fir);
            k--;
            while(k--)
            {
                scanf("%d",&tmp);
                merge(fir,tmp);
            }
        }
        int ex=findfather(0);
        for(int i=0;i<n;i++)
            if(findfather(i)==ex) ans++;
        printf("%d\n",ans);
    }
    return 0;
}

(似乎这个时候代码还很丑)

时间: 2024-10-05 00:17:40

kuangbin_UnionFindB (POJ 1611)的相关文章

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 Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22217   Accepted: 10805 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

kuangbin专题五:B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 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 f

B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 61692   Accepted: 29146 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mi

【原创】poj ----- 1611 The Suspects 解题报告

题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24253   Accepted: 11868 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recogni

【裸的并查集】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