【九度】题目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
****************************************************************/

HDOJ-1213 How Many Tables(包含题意)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • gui951753
  • gui951753
  • 2015年07月30日 10:10
  • 281

矩阵十大经典题目之八-hdu-2157-How many ways??

题目大意:给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值 把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=...
  • rowanhaoa
  • rowanhaoa
  • 2014年03月13日 01:00
  • 2585

HDU 1213 How Many Tables (并查集,连通分支数,两种方式)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • hurmishine
  • hurmishine
  • 2016年06月09日 23:12
  • 3256

微软2013暑假实习生笔试题

部分题目答案不确定,会持续更新…… 1. Which of the following calling convention(s) support(s) supportvariable-length...
  • fengbingyang
  • fengbingyang
  • 2013年04月06日 20:20
  • 11295

【九度OJ】题目1445:How Many Tables 解题报告

【九度OJ】题目1445:How Many Tables 解题报告标签(空格分隔): 九度OJ原题地址:http://ac.jobdu.com/problem.php?pid=1445题目描述:Tod...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017年03月10日 14:53
  • 351

题目1445:How Many Tables 九度OJ

题目1445:How Many Tables 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1779 解决:1078 题目描述: ...
  • qq_34447388
  • qq_34447388
  • 2017年05月02日 13:40
  • 100

九度OJ-1445:How Many Tables

用并查集求连通分量数。与上题完全一样,不赘述。 题目描述: Today is Ignatius' birthday. He invites a lot of friends. Now ...
  • u012963208
  • u012963208
  • 2017年02月09日 01:50
  • 120

HDOJ 题目1213 How Many Tables(并查集基础)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • yu_ch_sh
  • yu_ch_sh
  • 2014年08月05日 23:35
  • 326

HDU 1213 How Many Tables(并查集入门模板题)

C - How Many Tables HDU - 1213 Today is Ignatius' birthday. He invites a lot of friends. Now it's d...
  • qq_41444888
  • qq_41444888
  • 2018年01月03日 12:31
  • 301

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

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • zugofn
  • zugofn
  • 2016年08月01日 20:20
  • 470
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【九度】题目1445:How Many Tables
举报原因:
原因补充:

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