zoukankan      html  css  js  c++  java
  • 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
    ",sum);
        }
        return 0;
    }
    

      

  • 相关阅读:
    python3 基础数据类型之列表及其操作方法
    python3 逻辑运算符
    python3 内置函数
    python3 装饰器
    python3 变量作用域
    python3 参数以及函数的传参
    python3 函数基础
    洛谷P3379倍增LCA
    洛谷P3375KMP字符串匹配
    洛谷P2613有理数取余
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3833823.html
Copyright © 2011-2022 走看看