zoukankan      html  css  js  c++  java
  • hdu 1213 How Many Tables

    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 <algorithm>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <string>
     7 #include <cstdlib>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <vector>
    12 #include <map>
    13 #include <list>
    14 #include <iomanip>
    15  #include <fstream>
    16 
    17 using namespace std;
    18 int fa[1009];
    19 //这题一看就感觉和寒假的poj 2524异曲同工。都是普通的并查集 
    20 int get(int x)//找朋友总代表 
    21 {
    22     int root=x;
    23     while(fa[root]!=root)//不断向上寻找根节点 
    24         root=fa[root];
    25      
    26     int t=x,temp;
    27     while(fa[t]!=root)//自底(x)向上(root)更新一波 
    28     {
    29         temp=fa[t];//感觉swap函数并不可靠,之前用了换成这样朴素的交换就从WA变成AC了 
    30         fa[t]=root;
    31         t=temp;
    32     }
    33      return root;     
    34 }
    35 
    36 void join(int x,int y)//保存朋友关系 
    37 {
    38     int gx=get(x),gy=get(y);
    39     if(gx!=gy)//x 和 y的根节点不同 
    40         fa[gx]=gy;//那么这两者都归入到x根节点的根节点(大概就是最顶端的那个) 
    41 }
    42 int main()
    43 {
    44     int t;
    45     scanf("%d",&t);
    46     while(t--)
    47     {
    48         int n,m,x,y,cnt=0;
    49         scanf("%d%d",&n,&m);
    50         for(int i=1;i<=n;++i)
    51         {
    52             fa[i]=i;
    53         }
    54         for(int i=1;i<=m;++i)
    55         {
    56             scanf("%d%d",&x,&y);
    57             join(x,y);
    58         }
    59         for(int i=1;i<=n;++i)
    60         {
    61             if(get(i)==i)
    62                 cnt++;
    63         }
    64         printf("%d
    ",cnt);
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    从头来之【图解针对虚拟机iOS开发环境搭建】 (转)
    换工作?请记得8、18、48与72这四个密码(转)
    php
    linux上svn连接visual svn server时ssl鉴权失败,问题解决(转)
    javamail发送邮件的简单实例(转)
    稀疏矩阵
    Redis11种Web应用场景
    说说ShellExecuteEx
    怎样从host之外连接到docker container
    hadoop日志分析
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11201681.html
Copyright © 2011-2022 走看看