【九度】题目1444:More is better

108 篇文章 0 订阅
102 篇文章 5 订阅
题目地址:http://ac.jobdu.com/problem.php?pid=1444
题目描述:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入:

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出:

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入:
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
样例输出:
4
2

基本思路:
       本题需要求解的是可以留下来的最多的学生个数,这些学生要相互认识,就是并查集了。
       并查集求出parent[i],累加即可。
C++ AC代码

#include <stdio.h>
const int maxm = 10000002;
int parent[maxm];
int num[maxm];
int n;
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 max(int a, int b){
    return a > b ? a : b;
}
 
int main(){
    while(scanf("%d",&n) != EOF){
        for(i = 1; i < maxm; i++){
            parent[i] = i;
            num[i] = 1;
        }
        for(i = 0 ; i < n ; i++){
            int a, b;
            scanf("%d%d",&a,&b);
            unionTwo(a,b);
        }
        if(n == 1){
            printf("%d\n",2);
            continue;
        }
 
        int maxNum = 1;
        for (i = 1; i < maxm; i++) {  
            maxNum = max(++num[findParent(i)] , maxNum);
        }   
        printf("%d\n",maxNum - 1);
         
    }
    return 0;
}
 
/**************************************************************
    Problem: 1444
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:1190 ms
    Memory:79144 kb
****************************************************************/

Java AC

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
    
public class Main {
    /*
     * 1444
     */
    private static int []parent = new int[10000001];
    private static int []num = new int[10000001];
       
    public static void main(String[] args) throws Exception{
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) st.nval;
            if (n == 1) {
                System.out.println(2);
            }else {
                for (int i = 1; i < 10000000; i++) {
                    parent[i] = i;
                    num[i] = 1;
                }
                   
                for (int i = 0; i < n; i++) {
                    st.nextToken() ;
                    int f = (int) st.nval;
                    st.nextToken() ;
                    int t = (int) st.nval;
                    union(f ,t );
                }
                int max = 1;
                for (int i = 1; i < 10000000; i++) {
                    max = Math.max(++num[findParent(i)], max);
                }
                   
                System.out.println(max-1);
            }
        }
    }
      
    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: 1444
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:2140 ms
    Memory:105024 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值