kuangbin 并查集 C - How Many Tables HDU - 1213

How Many Tables  hdu-1213

思路:并查集之后找有几个集

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5;

int father[maxn];
void init(int n)
{
    for(int i=1;i<=n;i++)
        father[i] = i;
}
int find(int x)
{
    if(father[x] == x)
        return x;
    else
        return father[x] = find(father[x]);
}
void combine(int x,int y)
{
    x = find(x);
    y = find(y);
    if(x != y)
        father[x] = y;
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       int n,m;
       scanf("%d %d", &n,&m);
       init(n);
       for(int i=0;i<m;i++)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           combine(a,b);
       }
       int res = 0;
       for(int i=1;i<=n;i++)
           if(i == find(i))
               res++;
       printf("%d\n",res);
   }
}

原文地址:https://www.cnblogs.com/smallhester/p/10316036.html

时间: 2024-11-02 10:44:18

kuangbin 并查集 C - How Many Tables HDU - 1213的相关文章

kuangbin专题五:C - How Many Tables HDU - 1213

C - How Many Tables HDU - 1213 Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends

并查集(逆序处理):HDU 5652 India and China Origins

India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 676    Accepted Submission(s): 227 Problem Description A long time ago there are no himalayas between India and China, the

How Many Tables HDU - 1213

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strang

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断连接两点的边,query为查询某星球能不能向它人呼叫支援. 还是需要离线逆向并查集求解.思路和HDU 4496很相似,但是此处不一定是把所有边都删去,所以需要删边的情况建立出最终的状态.因为N可以到1e4,所以可以用map嵌套map的方式记录过程中被删去的边,最后再根据删除情况建立状态. 在合并时需

跟着chengyulala刷题之[kuangbin带你飞]之&#39;并查集&#39;专题/斜眼笑

[kuangbin带你飞] 专题1-23 https://vjudge.net/article/187 专题五 并查集 POJ 2236 Wireless Network  http://poj.org/problem?id=2236POJ 1611 The Suspects  http://poj.org/problem?id=1611HDU 1213 How Many Tables  http://acm.hdu.edu.cn/showproblem.php?pid=1213HDU 3038

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

Hdoj 1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16590 Accepted Submission(s): 8117 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner tim

hdu1213 How Many Tables(并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17946    Accepted Submission(s): 8822 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn