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

    How Many Tables
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 44432    Accepted Submission(s): 22199

    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

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <climits>
     8 #include <algorithm>
     9 #define INF 0xffffff
    10 using namespace std;
    11 
    12 int n, m, my_pre[1010];
    13 
    14 int my_find(int x)
    15 {
    16     int temp = x;
    17     while (temp != my_pre[temp]) temp = my_pre[temp];
    18     int i = x, j;
    19     while (temp != my_pre[i])
    20     {
    21         j = my_pre[i];
    22         my_pre[i] = temp;
    23         i = j;
    24     }
    25     return temp;
    26 }
    27 
    28 int main()
    29 {
    30     int t;
    31     scanf("%d", &t);
    32 
    33     while (t --)
    34     {
    35         int my_cnt = 1, my_first_temp;
    36         scanf("%d%d", &n, &m);
    37         for (int i = 1; i <= n; ++ i)
    38             my_pre[i] = i;
    39 
    40         for (int i = 1; i <= m; ++ i)
    41         {
    42             int a, b, n1, n2;
    43             scanf("%d%d", &a, &b);
    44             n1 = my_find(a), n2 = my_find(b);
    45             if (n1 != n2)
    46                 my_pre[n1] = n2;
    47         }
    48 
    49         my_first_temp = my_find(1);
    50         for (int i = 2; i <= n; ++ i)
    51             if (my_find(i) != my_first_temp)
    52                 my_pre[my_find(i)] = my_first_temp, ++ my_cnt;
    53 
    54         printf("%d
    ", my_cnt);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    手动封装 时间选择器(只显示时分)
    页面跳转问题-button 确定提交按钮
    java String类型转 java.sql.time类型
    Mysql 时间处理
    微信-商城商品的图文/商品链接分享(后台数据合成图片+二维码生成)
    np.nan 1.0 2.0 df.apply
    pandas math.isnan
    df.apply
    pandas多条件行数据筛选
    pandas cumsum/sum calc percentage
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9445711.html
Copyright © 2011-2022 走看看