• hdu 3172 Virtual Friends (并查集)


    Virtual Friends

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


    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 3047 2818 2473 
     
     1 //875MS     1828K    1063B     G++
     2 /*
     3 
     4     题意:
     5          给出n个朋友关系,朋友的朋友也是朋友,求每组关系的圈子的人数
     6          
     7     并查集+STL:
     8         这种做法有点小暴力,不过没想到更好的方法。
     9         要使用并查集的路径压缩,每次更新把全部点更新到一个点上,
    10     然后更新该改点的人数就行了。然后直接输出该点的人数。     
    11       
    12 
    13 */
    14 #include<iostream>
    15 #include<string>
    16 #include<map>
    17 using namespace std;
    18 int set[100005];
    19 int num[100005];
    20 int find(int x) 
    21 {
    22     int r=x;
    23     while(set[r]!=r)
    24         r=set[r];
    25     int i=x;
    26     while(i!=r){  //路径压缩 
    27         int j=set[i];
    28         set[i]=r;
    29         i=j;    
    30     }
    31     return r;
    32 }
    33 void merge(int a,int b)
    34 {
    35     int x=find(a);
    36     int y=find(b);
    37     if(x!=y){
    38         set[y]=x;
    39         num[x]+=num[y];
    40         printf("%d
    ",num[x]);
    41     }else{
    42         printf("%d
    ",num[x]);
    43     }
    44 }
    45 int main(void)
    46 {
    47     int t,n;
    48     char a[25],b[25];
    49     while(scanf("%d",&t)!=EOF)
    50     while(t--)
    51     {
    52         map<string,int>M;
    53         M.clear();
    54         for(int i=1;i<=100000;i++){
    55             set[i]=i;num[i]=1;
    56         }
    57         scanf("%d",&n);
    58         int m=1;
    59         for(int i=0;i<n;i++){
    60             scanf("%s%s",a,b);
    61             if(M[a]==0) M[a]=m++;
    62             if(M[b]==0) M[b]=m++;
    63             merge(M[a],M[b]);
    64         } 
    65     }
    66     return 0;
    67 }
    68 
    69 /*
    70 
    71 6
    72 3
    73 1 2
    74 2 3
    75 3 4
    76 5
    77 1 2
    78 2 3
    79 3 4
    80 4 4
    81 5 1
    82 
    83 
    84 */
  • 相关阅读:
    分解质因数
    记忆化搜索
    BFS(广度优先搜索)
    DFS(深度优先搜索)
    A*算法
    最小生成树
    最近公共祖先(LCA)问题
    k短路算法
    次短路问题
    拓扑排序
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3621771.html
Copyright © 2020-2023  润新知