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

      

  • 相关阅读:
    leetcode5 Longest Palindromic Substring
    leetcode17 Letter Combinations of a Phone Number
    leetcode13 Roman to Integer
    leetcode14 Longest Common Prefix
    leetcode20 Valid Parentheses
    leetcode392 Is Subsequence
    leetcode121 Best Time to Buy and Sell Stock
    leetcode198 House Robber
    leetcode746 Min Cost Climbing Stairs
    tomcat下使用druid配置jnid数据源
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3833823.html
Copyright © 2011-2022 走看看