• HDU 3172 Virtual Friends (并查集节点统计)


    Virtual Friends

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


    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
     

    题意:A和B是好朋友B和C是好朋友,那么C和A就是好朋友,求每次输入两个人的关系之后,好朋友的人数

    分析:统计并查集内节点的个数

    只用根节点记录人数即可

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=100000+5;
    int p[MAXN],res,cnt[MAXN];
    int kase,n,sum;
    map<string,int> mat;
    
    void init()
    {
        for(int i=1;i<=MAXN;i++) {p[i]=i;cnt[i]=1;}
        mat.clear();
        sum=1;
    }
    
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    
    int Union(int u,int v)
    {
        int x=findfa(u);
        int y=findfa(v);
        if(x!=y)
        {
            p[x]=y;
            cnt[y]+=cnt[x];
        }
        return cnt[y];
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d",&kase)!=EOF)
        {
            while(kase--)
            {
                scanf("%d",&n);
                init();
                while(n--)
                {
                    char str1[30],str2[30];
                    scanf("%s %s",str1,str2);
                    if(!mat[str1]) mat[str1]=sum++;
                    if(!mat[str2]) mat[str2]=sum++;
    
                    res=Union(mat[str1],mat[str2]);
    
                    printf("%d
    ",res);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    HGOI 20200724
    HGOI 20200722
    [USACO Open08]牛的邻居Cow Neighborhoods解题报告
    [USACO Jan07]考试Schul解题报告
    [CF 249D]Donkey and Start解题报告
    [CF 321D]Ciel and Flipboard解题报告
    [CF 294D]Shaass and Painter Robot解题报告
    [CF 297E]Mystic Carvings解题报告
    [CF 306E]Levko and Game题解翻译
    [CF 316F3]Suns and Rays解题报告
  • 原文地址:https://www.cnblogs.com/clliff/p/4694393.html
Copyright © 2020-2023  润新知