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 }
  • 相关阅读:
    越来越火的5G公网专用,到底是啥意思?
    看懂IPv6+,这篇就够了
    今年诺贝尔奖得主居然把这事研究清楚了:学历和收入到底有多大关系?
    华为发布《智能世界2030》报告,多维探索未来十年趋势
    每天一个离职小技巧
    Deepsort + Yolo 实现行人检测和轨迹追踪
    “5G+AI”到底有啥用?这篇漫画告诉你答案…
    全国多地拉闸限电 背后到底怎么了?
    【Ajax请求 】- 1.1前端和后台数据交互的说明
    【Web API系列教程】3.3 — 实战:处理数据(建立数据库)
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9445711.html
Copyright © 2011-2022 走看看