zoukankan      html  css  js  c++  java
  • 杭电3172--Virtual Friends(并查集+map)

    Virtual Friends

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


    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
     

     

    Recommend
    chenrui   |   We have carefully selected several similar problems for you:  3038 3234 1829 3047 2818 
    RE: 同一集合中节点个数;
        (并查集)转化思想 ,利用<map>把字符串转化为数字,还有他的存储功能;
     1 #include <map>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <iostream>
     5 using namespace std;
     6 char a[25], b[25];  int father[200020], num[200020];
     7 map<string, int> mmap;
     8 void init()
     9 {
    10     for(int i = 1; i <200020; i++)
    11     {
    12         father[i] = i;
    13         num[i]  =1;
    14     }
    15 }
    16 int find(int a)
    17 {
    18     while(a != father[a])
    19         a = father[a];
    20     return a;
    21 }
    22 void mercy(int a, int b)  //加了Yh,,过了, 
    23 {
    24     int q = find(a);
    25     int p = find(b);
    26     if(q != p)
    27     {
    28         if(num[q] > num[p])
    29         {
    30             father[p] = q;
    31             num[q] +=num[p];
    32             printf("%d
    ", num[q]);
    33         }
    34         else
    35         {
    36             father[q] = p;
    37             num[p] += num[q]; 
    38             printf("%d
    ", num[p]);
    39         }
    40     }
    41     else 
    42          printf("%d
    ", num[p]);
    43 }
    44 int main()
    45 {
    46     int m, t, num;
    47     while(cin >> t)
    48     {
    49         while(t--)
    50         {
    51             
    52             init();  num = 1;
    53             mmap.clear();
    54             scanf("%d", &m);
    55             //for(int i = 1; i <= m; i++)
    56             while(m--)
    57             {
    58                 scanf("%s %s", a, b);
    59                 if(!mmap[a]) mmap[a] = num++;
    60                 if(!mmap[b]) mmap[b] = num++;
    61                 mercy(mmap[a], mmap[b]);
    62             }
    63         }
    64     }
    65     return 0;
    66 }
     
     
  • 相关阅读:
    lufylegendRPG游戏引擎 Yorhom's Game Box
    讨论交流 Yorhom's Game Box
    货币之间的大小写转换
    Unreal3的D3D渲染器部分
    Linxu宿主目录
    用于主题检测的临时日志(b25e234297d442ccba394dd2241308d2 3bfe001a32de4114a6b44005b770f6d7)
    Linux命令 文件命名规则 基础
    C#_GDI_文章粗略整合
    由IDisposable接口导致的using使用 以及using的其他用法
    ADO.NET基础备忘1_SqlConnection SqlCommand SqlDataReader
  • 原文地址:https://www.cnblogs.com/soTired/p/4707134.html
Copyright © 2011-2022 走看看