• HDU 1213 How Many Tables


    How Many Tables

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

    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
     
    Author
    Ignatius.L
     
    Source
     
    #include<stdio.h>
    #include<string.h>
    
    const int maxn=1010;
    
    int n,m,ans;
    int father[maxn],rank[maxn];
    
    void makeSet(int n){
        for(int i=1;i<=n;i++){
            father[i]=i;
            rank[i]=1;
        }
    }
    
    int findSet(int x){
        if(x!=father[x]){
            father[x]=findSet(father[x]);
        }
        //printf("father[%d]=%d\n",x,father[x]);
        return father[x];
    }
    
    void Union(int a,int b){
        //printf("a=%d b=%d\n",a,b);
        int x=findSet(a);
        int y=findSet(b);
        if(x==y){
            //ans--;
            //printf("ans=%d x=%d y=%d \n",ans,x,y);
            return ;
        }
        ans--;
        if(rank[x]<=rank[y]){
            father[x]=y;
            rank[y]+=rank[x];
        }else{
            father[y]=x;
            rank[x]+=rank[y];
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            makeSet(n);
            int a,b;
            ans=n;
            for(int i=0;i<m;i++){
                scanf("%d%d",&a,&b);
                Union(a,b);
            }
            printf("%d\n",ans);
        }
        return 0;
    }
    Recommend
    Eddy
  • 相关阅读:
    有趣的linux指令
    Linux——文件打包与压缩
    linux点滴记录
    不归零法编码、曼彻斯特编码和差分曼彻斯特编码
    MySQL点滴记录
    hdu 1200 To and Fro(简单模拟或DP)
    hdu 1081 To The Max(dp+化二维为一维)
    抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
    抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
    hdu 1159 Common Subsequence(最长公共子序列 DP)
  • 原文地址:https://www.cnblogs.com/jackge/p/2965945.html
Copyright © 2020-2023  润新知