【九度】题目1445:How Many Tables

原创 2013年12月06日 12:50:23
题目描述:

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.

输入:

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.

输出:

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入:
2
5 3
1 2
2 3
4 5

5 1
2 5
样例输出:
2
4

题目大意:
        今天是Ignatius的生日,他邀请了很多人来。现在是晚饭时间了,要安排座位。但是陌生的朋友不愿意坐在一桌。我们假设A认识B,B认识C,那么A就认识C,他们就相互认识。现在有一些人,给出他们之间的关系,请你告诉Ignatius需要准备几张桌子。
解题思路:
     并查集的典型应用。
    1、将每个人的父节点设为他自己。
    2、合并,修改父节点。
    3、查找父节点,如果parent[i] = i就表示是一个集合。
    4、统计集合个数。
这个题目和题目1526:朋友圈的解法一致。

C++ AC代码

#include <stdio.h>
const int maxn = 1002;
int parent[maxn];
int n;
int m;
int i;
  
int findParent(int f) {  
    while(parent[f] != f){
        f = parent[f];
    }
    return f;
}  
  
void unionTwo(int f, int t) {  
              
    int a = findParent(f);  
    int b = findParent(t);  
    if (a == b) return;   
    if (a > b) {     
        parent[a] = b;     
    } else {  
        parent[b] = a;   
    }  
}  
 
int main(){
    int t;
    scanf("%d",&t);
    while( t > 0 ){
        t--;
        scanf("%d%d",&n,&m);
        for(i = 1; i < n+1; i++){
            parent[i] = i;
        }
        for(i = 0 ; i < m ; i++){
            int a, b;
            scanf("%d%d",&a,&b);
            unionTwo(a,b);
        }
        for (i = 1; i < n+1; i++) {  
            parent[i] = findParent(i);  
        }
        int num = 0;
        for(i = 1; i < n+1; i++){
            if(parent[i] == i){
                num ++;
            }
        }
        printf("%d\n",num);
    }
    return 0;
}
/**************************************************************
    Problem: 1445
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1024 kb
****************************************************************/

Java AC代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashSet;
import java.util.Set;
  
public class Main {
    /*
     * 1445
     */
    private static int parent[];
    public static void main(String[] args) throws Exception{
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        st.nextToken();
        int num = (int) st.nval;
        while (num > 0) {
            st.nextToken();
            int n = (int) st.nval;
            st.nextToken();
            int m = (int) st.nval;
            parent = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                parent[i] = i;
            }
            for (int i = 0; i < m; i++) {
                st.nextToken();
                int f = (int) st.nval;
                st.nextToken();
                int t = (int) st.nval;
                union(f, t);
            }
            for (int i = 1; i < n + 1; i++) {
                parent[i] = findParent(i);
            }
            Set<Integer> numSet = new HashSet<Integer>();
            for (int i = 1; i < n + 1; i++) {
                numSet.add(parent[i]);
            }
            System.out.println(numSet.size());
            num--;
        }
    }
    private static void union(int f, int t) {
        int a = findParent(f);
        int b = findParent(t);
        if (a == b) return; 
        if (a > b) {   
            parent[a] = b;   
         } else {
            parent[b] = a; 
         }
    }
    private static int findParent(int f) {
        while (parent[f] != f) {
            f = parent[f];
        }
        return f;
    }
}
/**************************************************************
    Problem: 1445
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:190 ms
    Memory:24248 kb
****************************************************************/
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

题目1445:How Many Tables 九度OJ

题目1445:How Many Tables 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1779 解决:1078 题目描述: ...

九度OJ-1445:How Many Tables

用并查集求连通分量数。与上题完全一样,不赘述。 题目描述: Today is Ignatius' birthday. He invites a lot of friends. Now ...

HDU1213 How Many Tables(并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...

HDU 1213 How Many Tables? (简单并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU1213 How Many Tables 解题报告

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...

hdoj1213How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

【HDU-1213】How Many Tables

点击打开链接 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327...

ACM-并查集- How Many Tables-HDU - 1213

D - How Many Tables Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner t...

杭电acm 1213 (How many tables)

//思路:把所有认识的朋友组成一棵树,只要知道有几棵树,就知道需要几张桌子了。(这里的树为了方便查找根,将子节点指向根节点) 初始化时将所有树的根设为-1; //通过依次读入认识的朋友(a,b),利...

HDU How Many Tables

Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)