zoukankan      html  css  js  c++  java
  • hdu 1856

    More is better

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
    Total Submission(s): 21574    Accepted Submission(s): 7866


    Problem Description
    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.
     
    Input
    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)
     
    Output
    The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
     
    Sample Input
    4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
     
    Sample Output
    4 2
     
    我想尽了一切办法还是超时了 。。别人用 C语言开个 10000000的数组都不会超 。。。我用字典树优化了一下都超了。。。
    //超时了
    package 并查集;
    
    import java.util.Scanner;
    
    class Trie_1856 {
        int key = 1;
        private Node root;
    
        public Trie_1856() {
            root = new Node();
        }
    
        int insert(String str) {
            Node t = root;
            int idx;
            for (int i = 0; i < str.length(); i++) {
                if (t.nodes[str.charAt(i)-'0'] == null) {
                    Node node = new Node();
                    t.nodes[str.charAt(i)-'0'] = node;
                }
                t = t.nodes[str.charAt(i)-'0'];
            }
            if (t.id == 0)
                t.id = key++;
            return t.id;
        }
    
        class Node {
            Node[] nodes;
            int id;
    
            public Node() {
                id = 0;
                nodes = new Node[10];
            }
        }
    }
    
    public class hdu_1856 {
        static int[] father=new int[10000005];
        static int[] friend= new int[10000005];
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                int n = sc.nextInt();
                if (n == 0) {
                    System.out.println(1);
                    continue;
                }
                Trie_1856 tree = new Trie_1856();
                for (int i = 1; i < 10000005; i++) {
                    friend[i] = 1;
                    father[i] = i;
                }
                int ans = 0;
                while (n-- > 0) {
                    String a = sc.next();
                    String b = sc.next();
                    int k1 = tree.insert(a);
                    int k2 = tree.insert(b);
                    int x = find(k1);
                    int y = find(k2);
                    if (x != y) {
                        father[x] = y;
                        friend[y] += friend[x];
                        if (ans < friend[y])
                            ans = friend[y];
                    }
                }
                System.out.println(ans);
            }
        }
    
        private static int find(int x) {
            if (x == father[x])
                return x;
            return find(father[x]);
        }
    }
  • 相关阅读:
    ZOJ 1649: Rescue(BFS)
    UVA
    hdu2458:Kindergarten (最大独立集)
    hdu3829:Cat VS Dog (最大独立集)
    Java 泛型
    request.getParameter() 和request.getAttribute() 区别
    Solr版本安装部署指南
    java.sql.SQLException: Incorrect string value: 'xE6x88x91xE7x9Ax84...' for column 'groupName'
    Incorrect string value: 'xF0x9Fx98x84xF0x9F
    java里面byte数组和String字符串怎么转换
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5320127.html
Copyright © 2011-2022 走看看