关闭

【九度】题目1445:How Many Tables

630人阅读 评论(0) 收藏 举报
分类:
题目描述:

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
****************************************************************/
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:255705次
    • 积分:5156
    • 等级:
    • 排名:第5350名
    • 原创:263篇
    • 转载:16篇
    • 译文:0篇
    • 评论:47条
    博客专栏
    最新评论