• 并查集之 How Many Tables


    How Many Tables

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


    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
     
    Recommend
    Eddy
     
    这题做得还是挺顺手的 呵呵  求连通分量的个数  water过了
     
    View Code
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 #define Max  1005
     5 int p[Max],flag[Max],n;
     6 
     7 void init()
     8 {
     9     for (int i=1;i<=n;i++)
    10     {
    11         p[i]=i;
    12         flag[i]=0;
    13     }
    14 }
    15 
    16 int find(int x)
    17 {
    18     if (x!=p[x])
    19     {
    20         p[x]=find(p[x]);
    21     }
    22     return p[x];
    23 }
    24 
    25 void uion(int a,int b)
    26 {
    27     a=find(a);
    28     b=find(b);
    29     if (a!=b)
    30     {
    31         p[a]=b;
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     int T,m,a,b,i,sum=0;
    38     cin>>T;
    39     while (T--)
    40     {
    41         cin>>n>>m;
    42         init();
    43         while (m--)
    44         {
    45             scanf("%d%d",&a,&b);
    46             flag[a]=flag[b]=1;
    47             uion(a,b);
    48         }
    49         sum=0;
    50         for (i=1;i<=n;i++)
    51         if (p[i]==i||!flag[i])    
    52                 sum++;        
    53         cout<<sum<<endl;
    54     }
    55     return 0;
    56 }
     
     
  • 相关阅读:
    js Array的方法及属性总结
    js 继承
    js 判断数据类型
    序列化和反序列化
    express 常用方法和插件
    node 常用的对象
    node.js 守护进程
    CentOS7安装Python3.8.1和ansible
    MAC终端终极美化方案
    Linux之top命令详解
  • 原文地址:https://www.cnblogs.com/wujianwei/p/2588115.html
Copyright © 2020-2023  润新知