zoukankan      html  css  js  c++  java
  • How Many Tables--hdu1213(并查集)

    How Many Tables

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

    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<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #define maxn 10000
     5 using namespace std;
     6 int a;
     7 int per[maxn];
     8 void init()
     9 {
    10     int i;
    11     for(i=1;i<=a;i++)
    12     per[i]=i;//初始化数组
    13  } 
    14  int find(int x)//查找根节点
    15  {
    16      int t=x;
    17      while(t!=per[t])
    18      t=per[t];
    19      per[x]=t;//缩短路径
    20      return t;
    21  }
    22  void join(int x,int y)
    23  {
    24      int fx=find(x);
    25      int fy=find(y);
    26      if(fx!=fy)
    27          per[fx]=fy;
    28          else
    29              return ;
    30  }
    31  
    32  int main()
    33  {
    34      int b,n,c,d;
    35     scanf("%d",&n);
    36      while(n--)
    37      {
    38          
    39          int sum=0,i;
    40          scanf("%d%d",&a,&b);
    41          init();
    42          while(b--)
    43          {
    44              scanf("%d%d",&c,&d);
    45              join(c,d);
    46          }
    47          for(i=1;i<=a;i++)
    48          {
    49              if(per[i]==i)
    50              sum++;//查找根节点的个数51          }
    52          printf("%d
    ",sum);
    53      }
    54      return 0;
    55  }
  • 相关阅读:
    Linux下安装mysql5.7
    springcloud alibaba-sentinel流控规则简介
    springcloud alibaba-sentinel初始化
    springcloud alibaba-sentinel下载安装和运行
    Python内置函数
    Python生成器
    Python解析式
    Python模块-----日期模块
    Python内置数据结构----字典
    Linux配置免密登录
  • 原文地址:https://www.cnblogs.com/Eric-keke/p/4687476.html
Copyright © 2011-2022 走看看