How Many Tables——并查集模板题

题目链接

题意:

n个人参加晚宴;完全不认识的两个人不能被分配在同一餐桌;认识具有传递性:A认识B B认识C那么A和C也认识.

题解:

将认识两个人合并到同一集合最后统计有多少个不同的集合即可;

代码:

#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;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)f[i]=i;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            join(x,y);
        }
        int ans=0;
        for(int i=1;i<=n;i++)if(f[i]==i)ans++;
        printf("%d\n",ans);
    }
    return 0;
}

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

时间: 2024-08-27 17:36:05

How Many Tables——并查集模板题的相关文章

PAT甲题题解-1114. Family Property (25)-(并查集模板题)

题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h

poj 2524 求连通分量(并查集模板题)

求连通分量 Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0Sample Output Case 1: 1Case 2: 7 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include

hdu 1213 求连通分量(并查集模板题)

求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long

hdu 1232 变成生成树至少还要加几条边 (并查集模板题)

求一个图 变成生成树至少还要加几条边(成环的边要删掉,但不用统计) Sample Input4 2 //n m1 3//u v4 33 31 21 32 35 21 23 5999 00 Sample Output102998 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6

【HDU1231】How Many Tables(并查集基础题)

什么也不用说,并查集裸题,直接盲敲即可. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace s

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

poj 1182 食物链 并查集好题

挑战程序设计上有解答的并查集好题.把事件作为元素进行合并,举例:若输入1 2 3,意思就是把2,3归为同一类,至于归于哪一类并不需要去讨论,则把2属于A,3属于A这两件事件归为一类;2属于B,3属于B这两件事归为一类;2属于C,3属于C这两件事归为一类:若输入 2 2 3,由于A吃B,B吃C,C吃A,就把2属于A,3属于B这两件事情归为一类:以此类推.当检测到当前情况与之前正确的情况不符合,则错误的情况数加1. #include <iostream> #include <cstdio&g

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1

并查集模板

//普通并查集模板 #include<iostream> using namespace std; const int MAX=10004; int fat[MAX];//存放每个节点的根节点 //找x的根节点并且把路径上的每个节点的父节点改成根节点 int find(int x) //while找根节点 { int rt=x; while(fat[rt]!=rt) rt=fat[rt]; int i=x,j; while(i!=rt){ j=fat[i]; fat[i]=rt; i=j; }