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

    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. 

    InputThe 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.
    OutputFor 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 <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int f[N];
    35 int find1(int x)
    36 {
    37     if(f[x]!=x)
    38         f[x]=find1(f[x]);
    39     return f[x];
    40 }
    41 void And(int x,int y)
    42 {
    43     int xx,yy;
    44     xx=find1(x);
    45     yy=find1(y);
    46     if(xx!=yy)
    47         f[xx]=yy;
    48 }
    49 int main()
    50 {
    51     std::ios::sync_with_stdio(false);
    52     int t,n,m,a,b;
    53     cin>>t;
    54     while(t--){
    55         cin>>n>>m;
    56         for(int i=1;i<=n;i++)
    57             f[i]=i;
    58         for(int i=0;i<m;i++){
    59             cin>>a>>b;
    60             And(a,b);
    61         }
    62         int s=0;
    63         for(int i=1;i<=n;i++)
    64             if(find1(i)==i) s++;
    65         cout<<s<<endl;
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    多线程学习笔记第一篇
    当Visual Studio中win32控制台应用程序的注释也会生产代码时……
    博客行文及排版技法
    Debian Linux下的Python学习——安装python,vim
    onhashchange事件
    MyEclipse9 Maven开发Web工程 详细配置
    Java面向对象(上)
    lucene 的分析器(analyzer)与分词器(tokenizer)和过滤器(tokenfilter)
    java编程陷阱
    solr中文分词(mmseg4j)
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7277956.html
Copyright © 2011-2022 走看看