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]);
        }
    }
  • 相关阅读:
    Windows Azure Storage (17) Azure Storage读取访问地域冗余(Read Access – Geo Redundant Storage, RA-GRS)
    SQL Azure (15) SQL Azure 新的规格
    Azure China (5) 管理Azure China Powershell
    Azure China (4) 管理Azure China Storage Account
    Azure China (3) 使用Visual Studio 2013证书发布Cloud Service至Azure China
    Azure China (2) Azure China管理界面初探
    Azure China (1) Azure公有云落地中国
    SQL Azure (14) 将云端SQL Azure中的数据库备份到本地SQL Server
    [New Portal]Windows Azure Virtual Machine (23) 使用Storage Space,提高Virtual Machine磁盘的IOPS
    Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())的注意点
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5320127.html
Copyright © 2011-2022 走看看