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]);
        }
    }
  • 相关阅读:
    MySQL 数据恢复
    由 go orm 引发的探索
    beego 优雅重启
    2020年8月20日
    Linux 递归获取目录下所有满足条件的文件
    NET Core Kestrel部署HTTPS 一个服务器绑一个证书 一个服务器绑多个证书
    Flutter环境配置-windows
    Vue获取钉钉免登陆授权码(vue中的回调函数实践)
    【C#上位机必看】你们要的Iot物联网项目来了
    Windows Server系统部署MySQL数据库
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5320127.html
Copyright © 2011-2022 走看看