The Suspects——并查集求联通块数量的模板题

题目链接

题意:

n个学生分属m个团体,一个学生可以属于多个团体。一个学生疑似患病,则他所属的整个团体都疑似患病。已知0号疑似患病,以及每个团体都有哪些学生构成,求一共有多少个学生疑似患病

题解:

很经典的并查集的题目,找一个num[]数组记录每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的num值,就是0号学生所在团体的人数。

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+5;
int f[maxn];
int n,m;
int num[maxn];
int a[maxn];
int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}
void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        f[fx]=fy;
        num[fy]+=num[fx];
    }
}
int main()
{
    while(scanf("%d%d",&n,&m) && (n || m))
    {
        for(int i=0;i<n;i++)f[i]=i,num[i]=1;

        while(m--)
        {
            int k;
            scanf("%d",&k);
            for(int i=1;i<=k;i++)scanf("%d",&a[i]);
            for(int i=1;i<k;i++)join(a[i],a[i+1]);
        }
        int k=Find(0);
        printf("%d\n",num[k]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/j666/p/11609997.html

时间: 2024-08-11 20:37:26

The Suspects——并查集求联通块数量的模板题的相关文章

任意点~并查集求联通块

链接:https://www.nowcoder.com/acm/contest/84/C来源:牛客网 任意点 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可以改变方向. 请问至少需要加多少个点,使得点对之间互相可以到达. 输入描述: 第一行一个整数n表示点数( 1 <= n <= 100).第二行n行,

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is col

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

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

The Suspects(并查集求节点数)

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

分别利用并查集,DFS和BFS方法求联通块的数量

联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m行(输入数据到文件截止): 输出:第一行要求输出联通块的个数,并在第二行分别输出每个联通块中点的数量,每个数之间以一个空格隔开. 样例 15 31 42 53 5输出:2 2 3样列2 9 81 22 33 43 74 54 67 87 9输出: 19 如果不明白的话可以画图试试,最多花半个小时,要是早这样不

[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

C. Learning Languages 求联通块的个数

C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue&