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 }
     
     
  • 相关阅读:
    整理ASP.NET MVC 5各种错误请求[401,403,404,500]的拦截及自定义页面处理实例
    Sql Server 创建表添加说明
    c# 去除字符串中重复字符
    WebStorm中常用的快捷键及使用技巧
    git bash中的快捷键
    git bash here 的 ~/.bashrc 配置文件。和 vue/cli 3. 0 的 .vuerc文件(preset )
    右键菜单添加打开CMD选项
    Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
    vue和stylus在subline中显示高亮
    stylus入门教程,在webstorm中配置stylus
  • 原文地址:https://www.cnblogs.com/soTired/p/4707134.html
Copyright © 2011-2022 走看看