hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12538    Accepted Submission(s): 6145

Problem Description

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 strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input

2

5 3

1 2

2 3

4 5

5 1

2 5

Sample Output

2

4

========================================================

题意很容易懂,就是求总体有多少祖先,具体看代码吧

如果你什么都不知道,就看看这里的文章吧

http://zh.wikipedia.org/zh-cn/%E5%B9%B6%E6%9F%A5%E9%9B%86

http://www.cnblogs.com/cyjb/p/UnionFindSets.html

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>

#define Maxsize 1005
int uset[Maxsize];//祖先节点
int rank[Maxsize];

void Markset(int size)
{
    for(int i=1;i<=size;i++)//初始化
    {
        uset[i]=i;//每个点有独自的祖先
        rank[i]=0;
    }
}

int find(int x)//查找祖先
{
    if(x!=uset[x])
        uset[x]=find(uset[x]);//递归全指向祖先
    return uset[x];
}

void Union(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x == y)
    return ;
    if(x!=y)//不是一个家族
    {
        uset[x]=find(y);//合并,就是把y的祖先当x的孙子^^
    }
}

int main()
{
    int t,n,m;
    int a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        Markset(n);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            Union(a,b);//合并两家祖先
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(uset[i]==i)//查看有多少祖先
            sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}

  

hdu 1213 并查集入门

时间: 2025-01-17 07:29:58

hdu 1213 并查集入门的相关文章

hdu 1213 并查集 水

http://acm.hdu.edu.cn/showproblem.php?pid=1213 做到一道网赛题 2-sat可写 貌似并查集也可写  但是并查集做法没想到 先水几道并查集重新理解下然后再去做 学到的就一点  Father数组中有些值一直保持最初的father[x] == x  最终集合的个数可以通过这个判断 #include <cstdio> #include <cstring> #include <algorithm> #include <iostr

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu1272并查集入门

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 41540    Accepted Submission(s): 12811 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>