zoukankan      html  css  js  c++  java
  • 并查集之 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 }
     
     
  • 相关阅读:
    <转>反调试技巧总结原理和实现
    反调试功能<IsDebuggerPresent>
    通过取得MAC地址判断是否在VM中
    任何值得拥有的东西
    我的程序里
    吸引力法则之谜——十一条被遗忘的定律
    要成功,请忘掉自尊
    我是个只顾着想,却不去做的人
    现在有12个金币,其中一个有质量问题(或重或轻),还有一个无砝码的天平,让你称三次怎么样找到那个有质量问题的金币?
    惆怅
  • 原文地址:https://www.cnblogs.com/wujianwei/p/2588115.html
Copyright © 2011-2022 走看看