【九度】题目1444:More is better

题目地址: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
****************************************************************/

你可能感兴趣的:(【九度】题目1444:More is better)