zoukankan      html  css  js  c++  java
  • hdu 3172

    Virtual Friends

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7447    Accepted Submission(s): 2134


    Problem Description
    These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

    Your task is to observe the interactions on such a website and keep track of the size of each person's network.

    Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
     
    Input
    Input file contains multiple test cases.
    The first line of each case indicates the number of test friendship nest.
    each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
     
    Output
    Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.
     
    Sample Input
    1 3 Fred Barney Barney Betty Betty Wilma
     
    Sample Output
    2 3 4
     
    Source
     
     
    坑死我了。。开始以为是代码有问题,不停地WA,后来去参考别人的才发现是处理到文件尾。。。
    while(sc.hasNext){  //千万要写这一层
      int tcase = sc.nextInt();
      while(tcase-->0){
        int n = sc.nextInt();
        while(n-->0){
        }
      }
    }
     
    思路就是把字符串映射+并查集.
    我用了 TreeMap ,超时了,所以用了字典树
    //A与B是朋友 ,B与C是朋友 则 A和C是朋友 ,所以 A,B的朋友圈里面有三个人,此题就是问在A,B的朋友圈里面有多少人
    //题解: 字典树 +并查集
    
    import java.util.Scanner;
    
    class Trie_3127 {
        int key = 1;
        private Node root;
    
        public Trie_3127() {
            root = new Node();
        }
    
        int insert(String str) {
            Node t = root;
            int idx;
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
                    idx = str.charAt(i) - 'a';
                else
                    idx = str.charAt(i) - 'A' + 26;
                if (t.nodes[idx] == null) {
                    Node node = new Node();
                    t.nodes[idx] = node;
                }
                t = t.nodes[idx];
            }
            if (t.id == 0)
                t.id = key++;
            return t.id;
        }
    
        class Node {
            Node[] nodes;
            int id;
    
            public Node() {
                id = 0;
                nodes = new Node[52];
            }
        }
    }
    
    public class Main {
        static int[] father;
        static int[] friend;
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                int tcase = sc.nextInt();
                while (tcase-- > 0) {
                    Trie_3127 tree = new Trie_3127();
                    int n = sc.nextInt();
                    father = new int[2 * n + 1];
                    friend = new int[2 * n + 1];
                    for (int i = 1; i < 2 * n + 1; i++) {
                        father[i] = i; // i的根节点就是自己
                        friend[i] = 1; // i的朋友最初只有他自己一个
                    }
                    while (n-- > 0) {
                        String name1, name2;
                        name1 = sc.next();
                        name2 = sc.next();
                        int a = tree.insert(name1);
                        int b = tree.insert(name2);
                        // System.out.println(a+" "+b);
                        int x = find(a);
                        int y = find(b);
                        if (x != y) {
                            father[x] = y;
                            friend[y] += friend[x];
                        }
                        System.out.println(friend[y]);
                    }
                }
            }
    
        }
    
        private static int find(int a) {
            if (a == father[a])
                return a;
            return find(father[a]);
        }
    }
  • 相关阅读:
    使用正则匹配数字
    钻石和玻璃球游戏(钻石位置不固定)
    简单绘图
    未解决问题02
    Sqlite3 实现学生信息增删改查
    【Python】科赫雪花绘制
    【Python爬虫】抖音去水印
    【MATLAB】数学计算软件 MathWorks MATLAB R2020a 中文破解版
    【C语言】用指针作为形参完成数据的升序排列
    【C语言】数组名作函数参数完成数据的升序排列
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5319885.html
Copyright © 2011-2022 走看看