• hdu 1213(并查集模版题)


    How Many Tables

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


    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
     
    板子题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<string>
    12 #define eps 0.000000001
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int N=1000+100;
    17 int parent[N];
    18 int find(int x){
    19     int r=x;
    20     while(r!=parent[r])r=parent[r];
    21     int i=x;
    22     int j;
    23     while(parent[i]!=r){
    24         j=parent[i];
    25         parent[i]=r;
    26         i=j;
    27     }
    28     return r;
    29 }
    30 void Union(int x,int y){
    31     x=find(x);
    32     y=find(y);
    33     if(x<y)parent[x]=y;
    34     else if(x>y)parent[y]=x;
    35     if(x==y)return;
    36 }
    37 int main(){
    38     int t;
    39     scanf("%d",&t);
    40     while(t--){
    41         int n,m;
    42         int x,y;
    43         cin>>n>>m;
    44         for(int i=1;i<=n;i++)parent[i]=i;
    45         for(int i=1;i<=m;i++){
    46             cin>>x>>y;
    47             Union(x,y);
    48         }
    49         int ans=0;
    50         for(int i=1;i<=n;i++){
    51             if(parent[i]==i)ans++;
    52         }
    53         cout<<ans<<endl;
    54     }
    55 }
  • 相关阅读:
    springCloud 服务间相互调用
    不同环境下整合相同格式的文件
    Linux 安装mysql
    利用Maven打War包引入本地Jar包
    linux服务器部署项目详细步骤
    DM数据库导入dmp文件报大小写敏感问题解决办法
    synchronized 实现过程
    TypeScript学习笔记
    @RequestMapping注解详解
    常用的Linux命令
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6443046.html
Copyright © 2020-2023  润新知