HDU1213 How Many Tables

来源:互联网 发布:linux python环境变量 编辑:程序博客网 时间:2024/06/02 17:20

How Many Tables

                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                 Total Submission(s): 20906    Accepted Submission(s): 10350
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
25 31 22 34 5
5 12 5
Sample Output
24

    题意:了解彼此的人坐在一张桌子上,比如A知道B,B知道C,那么A与B,B与C,A与C都可以坐在一张桌上。在所有来的人中,问最少需要多少张桌子。这是一个简单的并查集,把熟悉的人合并在一起,在查询一共有几个集合。
java代码:
import java.util.*;import java.lang.*;import java.math.*;import java.util.*;
public class Main {static int N,M;static int[] node;static int[] rank;
public static void Init(int n){node = new int[1005];   //node[i]数组表示i的根rank = new int[1005];   //rank[i]表示i的深度for(int i = 1;i <= n;i++){  node[i] = i;        //初始化,每个结点的根是自己,深度为0rank[i] = 0;} }
public static int Find(int x){    //查询x的根结点if(x == node[x])return x;return node[x] = Find(node[x]);}
public static void Unite(int x,int y){  //和并x代表的集合和y代表的集合 ,把深度较小的集合合并到另外一个集合(减小查找力度)x = Find(x);y = Find(y);if(x == y)return; if(rank[x] <rank[y]){node[x] = y;}else{node[y] = x;if(rank[x] == rank[y]) rank[x]++; }}
public static void main(String[] args) throws Exception{int T;Scanner cin = new Scanner(System.in);T = cin.nextInt();int sum;while(T > 0){int i;int a,b;sum = 0;N = cin.nextInt();M = cin.nextInt();Init(N);for(i = 0;i < M;i++){a = cin.nextInt();b = cin.nextInt();Unite(a,b);}     for(i = 1;i<=N;i++){if(node[i] == i)sum++;}System.out.println(sum);T--;}  } }


C++代码:简单并查集,没有经过优化。

#include<stdio.h>#include<string.h>const int maxn=1005;int n,m;int parent[maxn];int FindRoot(int x){    if(parent[x] == x)        return x;    return FindRoot(parent[x]);}
//初始化结合void MakeSet(){    for(int i=0;i<maxn;i++)       parent[i]=i;   }void Union(int x,int y){    int xRoot=FindRoot(x);    int yRoot=FindRoot(y);    parent[xRoot]=yRoot;}int main(){    int v1,v2;    int nCase;    scanf("%d",&nCase);    while(nCase--)    {        scanf("%d%d",&n,&m);        MakeSet();        for(int i=0;i<m;i++)        {            scanf("%d%d",&v1,&v2);            Union(v1,v2);        }        int ans=0;        for(int i=1;i<=n;i++)        {            if(i==parent[i])                ans++;        }        printf("%d\n",ans);    }    return 0;}


 
0 0
原创粉丝点击