zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    python注释方法以及编码问题
    python数据类型和变量
    JavaScript必须了解的知识点总结。
    javaScript语法总结
    美图WEB开放平台环境配置
    变点问题的统计推新及其在全融中的应用 谭常春
    Structural breaks in time series
    多种单位根检验法的比较研究 房林邹卫星
    1-出口数据的平稳性分析
    时间序列中的结构突变与单位根检验
  • 原文地址:https://www.cnblogs.com/jackge/p/2965945.html
Copyright © 2011-2022 走看看