zoukankan      html  css  js  c++  java
  • 5.1.6 Virtual Friends

    Virtual Friends

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

    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

    思路:并查集吧,没什么好说的,学了学map,好好用啊!

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <string>
     7 #include <map>
     8 using namespace std;
     9 
    10 map<string,int> mp;
    11 const int maxn=200100;
    12 int f[maxn],sum[maxn],a,b,n,m,fx,fy,cnt;
    13 char s1[30],s2[30];
    14 
    15 void close()
    16 {
    17     fclose(stdin);
    18     fclose(stdout);
    19     exit(0);
    20 }
    21 
    22 int find(int k)
    23 {
    24     if (f[k]==k)
    25         return k;
    26     return f[k]=find(f[k]);
    27 }
    28 
    29 void merge(int x,int y)
    30 {
    31     fx=find(x);
    32     fy=find(y);
    33 //    printf("fx:%d fy:%d\n",fx,fy);
    34     if (fx!=fy)
    35     {
    36         f[fx]=fy;
    37     sum[fy]+=sum[fx];
    38     }
    39     printf("%d\n",sum[fy]);
    40 }
    41 
    42 void work()
    43 {
    44 }
    45 
    46 void init ()
    47 {
    48 freopen("vfriend.in","r",stdin);
    49 freopen("vfriend.out","w",stdout);
    50     int T;
    51      while (scanf("%d",&T)!=EOF)
    52      {
    53      while (T--)
    54      {
    55          mp.clear();
    56          memset(f,0,sizeof(f));
    57          memset(sum,0,sizeof(sum));
    58          scanf("%d",&n);
    59          cnt=0;
    60          for (int i=1;i<=2*n;i++)
    61          {
    62              f[i]=i;
    63              sum[i]=1;
    64          }
    65          for (int i=1;i<=n;i++)
    66          {
    67              scanf("%s %s",s1,s2);
    68              if (mp.find(s1)==mp.end())
    69              {
    70                  cnt++;
    71                  mp[s1]=cnt;
    72              }
    73              if (mp.find(s2)==mp.end())
    74              {
    75                  cnt++;
    76                  mp[s2]=cnt;
    77              }
    78              merge(mp[s1],mp[s2]);
    79          }
    80      }
    81      }
    82 }
    83 
    84 int main ()
    85 {
    86     init();
    87     work();
    88     close();
    89     return 0;
    90 }
  • 相关阅读:
    如何从GAC中拷贝文件出来
    寻找SPS2003的UI里文档或页面在数据库中的对应
    摸索WSS的service pack包的构成
    SharePoint Designer 2007可以打开SharePoint Portal Server 2003的站点么?
    什么是Query String
    升级SharePoint场的时候, 运行Configuration Wizard需要有什么顺序么?
    介绍一个免费的VS小插件 RockScroll
    记一次configuration wizard运行失败
    可以完成发送邮件功能的C#代码
    创建系统无法删除的文件夹 加密文件
  • 原文地址:https://www.cnblogs.com/cssystem/p/2911307.html
Copyright © 2011-2022 走看看