• Virtual Friends HDU


    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. 

    InputInput 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).OutputWhenever 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存一下,方便输出
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define eps 1e-10
    const int maxn=100100;
    map<string,int>p;
    int pre[maxn],ran[maxn],maxx;
    void init()
    {
        int i;
        for(i=1;i<maxn;++i)
        {
            pre[i] = i;
            ran[i]=1;
        }
    }
    
    int find(int x)
    {
        if(x!=pre[x])
        {
            pre[x] = find(pre[x]);
        }
        return pre[x];
    }
    
    void combine(int x,int y)
    {
        int fx = find(x);
        int fy = find(y);
        if(fx!=fy)
        {
            pre[fx] = fy;
            ran[fy] += ran[fx];
        }
    }
    
    
    int main()
    {
       
        int n,m;
        char name[30],na[30];
        while(scanf("%d",&n)!=EOF)
        {
            while(n--)
            {
            p.clear();
            init();
            int k=1;
                scanf("%d",&m);
            for(int i=0;i<m;i++)
            {
                scanf("%s %s",name,na);
                if(p.find(name)==p.end())
                    p[name]=k++;
                if(p.find(na)==p.end())
                    p[na]=k++;
                combine(p[name],p[na]);
                printf("%d
    ",ran[find(p[na])]);
            }
            }
        }
        return 0;
    }
  • 相关阅读:
    第六次作业
    第五次作业1
    java第三次作业
    JAVA 第二次作业
    JAVA第一次作业
    第 十一 次作业
    第十次作业
    第九次作业
    第八次作业
    第七次作业
  • 原文地址:https://www.cnblogs.com/smallhester/p/9500334.html
Copyright © 2020-2023  润新知