zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【板+背包】多重背包 HDU Coins
    【板+并查集判断连通性】并查集判断连通性
    【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle
    【区间筛】2017多校训练四 HDU6069 Counting Divisors
    【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing
    【链表】2017多校训练三 HDU 6058 Kanade's sum
    【带权并查集】HDU 3047 Zjnu Stadium
    【优先级队列】 Holedox Eating
    ajax学习笔记3-jQuery实现ajax(大拇指向上)
    ajax学习笔记2-JSON形式返回(大拇指向上)
  • 原文地址:https://www.cnblogs.com/smallhester/p/9500334.html
Copyright © 2011-2022 走看看