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 }
  • 相关阅读:
    [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...
    <yii 框架学习> yii 框架改为中文提示
    Yii 语言设置 中文提示信息
    yii新手在实例化models(controller调用models实化化)php warning错误
    yii CFormModel中的rules验证机制
    神舟优雅系列和神舟精盾系列哪个好?
    response.sendRedirect跳转 jsp:forward跳转
    jsp post/get中接处理
    jsp动作之 forward
    JDK eclipse selenium的安装以及环境变量的配置
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5605196.html
Copyright © 2011-2022 走看看