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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

    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<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<iomanip>
     6 #include<map>
     7 
     8 using namespace std;
     9 
    10 int fa[1005];
    11 bool flag[1005];
    12 
    13 int findf( int x ){
    14     int root = x, pos = x;
    15     while( root != fa[root] ){
    16         root = fa[root];
    17     }
    18     while( pos != root ){
    19         x = fa[pos];
    20         fa[pos] = root;
    21         pos = x;
    22     }
    23     return root;
    24 }
    25 
    26 int main(){
    27     ios::sync_with_stdio( false );
    28 
    29     int t, n, m, a, b, ans;
    30     cin >> t;
    31     while( t-- ){
    32         cin >> n >> m;
    33         for( int i = 1; i <= n; i++ ){
    34             fa[i] = i;
    35         }
    36         memset( flag, false, sizeof( flag ) );
    37         ans = 0;
    38 
    39         while( m-- ){
    40             cin >> a >> b;
    41             if( findf( a ) != findf( b ) ){
    42                 fa[findf( a )] = b;
    43             }
    44         }
    45 
    46         for( int i = 1; i <= n; i++ ){
    47             int temp = findf( i );
    48             if( !flag[temp] ){
    49                 ans++;
    50                 flag[temp] = true;
    51             }
    52         }
    53 
    54         cout << ans << endl;
    55     }
    56 
    57     return 0;
    58 }
  • 相关阅读:
    [GDOI2018]滑稽子图
    单位根反演学习笔记
    ODOO/OPENERP的网页模块QWEB简述
    odoo中的QWeb模板引擎
    项目管理)沟通管理
    从vc6升级到vc7的一些问题及解决方法
    vc++ 2005 发布程序
    颜色取反
    几个VC6.0到VC9.0的错误解决方案
    测试计划测试用例
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5605196.html
Copyright © 2011-2022 走看看