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

    Virtual Friends

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


    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
     格式坑爹,水死了;存根节点。。。。
    代码:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<map>
     4 #include<string>
     5 #define MAX(x,y) x>y?x:y
     6 const int MAXN=100005;
     7 using namespace std;
     8 int fri[MAXN],num,now[MAXN];
     9 void initial(){
    10     for(int i=1;i<=MAXN;++i)fri[i]=i,now[i]=1;
    11 }
    12 int find(int x){
    13     /*if(x==fri[x])return x;
    14      return fri[x]=find(fri[x]);*/
    15     int r=x;
    16      while(r!=fri[r])r=fri[r];
    17      int i=x,j;
    18      while(i!=r)j=fri[i],fri[i]=r,i=j;
    19      return r;
    20 }
    21 void merge(int x,int y){
    22     int f1,f2;
    23     f1=find(x);f2=find(y);
    24     if(f1!=f2){
    25         fri[f2]=f1;now[f1]+=now[f2];//now[f2]=now[f1];
    26         //printf("now[%d]=%d
    ",f1,now[f1]);
    27     }
    28 }
    29 int main(){
    30     int T,N;
    31     string A,B;
    32     while(~scanf("%d",&T)){
    33     while(T--){map<string,int>m;initial();num=0;
    34         scanf("%d",&N);
    35     while(N--){
    36         cin>>A>>B;
    37         if(m.find(A)==m.end())num++,m[A]=num;
    38         if(m.find(B)==m.end())num++,m[B]=num;
    39     //    printf("%d %d
    ",m[A],m[B]);
    40         merge(m[A],m[B]);
    41         printf("%d
    ",now[find(m[A])]);
    42     }
    43     }
    44         }
    45     return 0;
    46 }
    47 /*1
    48 7
    49 a b
    50 a a
    51 a c
    52 c g
    53 g h
    54 n n
    55 h n
    56 */
  • 相关阅读:
    本地存储 localStorage
    正则对象
    面向对象
    事件
    日期对象
    网易适配与淘宝适配
    自动把网页px单位转换成rem
    湖南省web应用软件(中慧杯)
    百度图片审核功能
    百度ai语音识别
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4685604.html
Copyright © 2011-2022 走看看