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;
    }
    

      

  • 相关阅读:
    java.lang.OutOfMemoryError: GC overhead limit exceeded
    Ural 1146 Maximum Sum(DP)
    [算法系列之四]优先级队列
    Python源代码--整数对象(PyIntObject)的内存池
    3星|何帆《猜测与偏见》:社科书评集
    4星|《认识经济》:全面系统的经济学普及读物,鸿篇巨制,价格超贵
    3星|《东方启动点》:民营企业家们的故事,故事多分析概括少
    2星|《大师的管理课》:畅销书作家们的35篇励志散文
    3.5星|《蓝海战略2》:实施蓝海战略的具体工具与方法。案例牵强且偏老旧
    2星|《读懂华为30年》:基于公开资料的整理和肤浅的发挥
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3833823.html
Copyright © 2011-2022 走看看