HDU 3172 - Virtual Friends(map+并查集)

Virtual Friends

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

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
University of Waterloo Local Contest 2008.09

题意:朋友的朋友也是朋友,题目给出两个是朋友的人名,要求输出这当前两人所在朋友圈的人数。很显然的并查集。
注意:题目是多组输入,输入的T EOF才算结束。(自己做的时候一直wa找了好久才找到这个问题)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#define ll long long
#define inf 0x3fffffff
const int maxn=200000+5;
using namespace std;

map<string,int> m;
int root[maxn];
int rank0[maxn];
int num;

void make_set(int n)
{
    for(int i=1;i<=2*n;i++)
    {
        root[i]=i;
        rank0[i]=1;
    }
}

int find_set(int n)
{
    if(n!=root[n])
    {
        root[n]=find_set(root[n]);
    }
    return root[n];
}

void union_set(int a,int b)
{
    int x=find_set(a);
    int y=find_set(b);
    if(x==y)
    {
        printf("%d\n",rank0[x]);
        return;
    }
    printf("%d\n",rank0[x]+rank0[y]);
    root[y]=x;
    rank0[x]+=rank0[y];
}

int main()
{
    int t,n,i,a,b;
    string n1,n2;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            make_set(n);
            num=1;
            m.clear();
            for(i=0;i<n;i++)
            {
                cin>>n1>>n2;
                if(!m.count(n1))
                    m[n1]=num++;
                if(!m.count(n2))
                    m[n2]=num++;
                a=m[n1];
                b=m[n2];
                //printf("%d %d\n",a,b);
                union_set(a,b);
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33472557/article/details/77203310