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
  • 相关阅读:
    Linux免密登录
    HDFS shell 常用命令
    zabbix4.4图表中文显示乱码解决办法
    安装zabbix-agent
    安装配置zabbix4.4
    elasticsearch插件sql安装
    dedecms调用头部文件 dede:include时页面出现一行空白的解决方案
    DedeCMS <=5.7 SP2 file_class.php 任意文件上传漏洞
    DedeCMS后台文件任意上传漏洞media_add.php的修改方法
    织梦DEDECMS任意文件上传漏洞与注入漏洞修复方法
  • 原文地址:https://www.cnblogs.com/jackge/p/2965945.html
Copyright © 2011-2022 走看看