HDU 3172 并查集-Virtual Friends

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

题意:


虚拟网络中有很多个网络好友,网络好友互相之间可以组成好友圈,每次给一对好友,问这一对好友所在的好友圈有多少人。


题解:


很简单的一道并查集题目。
如果不了解什么是并查集,可以到下面这个链接里面去看,我觉得讲得很好,第一次了解并查集就是看这个看懂的。
http://blog.csdn.net/qzc295919009/article/details/23306781
本题的困难在于如何将名字字符串对应一个数字并进行并查集的操作,我的解决方式是用map

cin >> a >> b;//输入两个字符串
if(ma.count(a) == 0)//如果不存在
{
    k1 = ma.size();//map的大小作为字符串的数字,不用再维护额外变量
    ma.insert(make_pair(a,ma.size()));
}
else
    k1 = ma[a];
if(ma.count(b) == 0)
{
    k2 = ma.size();
    ma.insert(make_pair(b,ma.size()));
}
else
    k2 = ma[b];

这样再结合基本的并查集操作,题目就解决了,完整AC代码如下。


#include 
#include 
#include 
#include 
#include 
#define MAXN 200010
using namespace std;

int fa[MAXN];
int sz[MAXN];
map<string,int> ma;

void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        fa[i] = i;
        sz[i] = 1;
    }
    ma.clear();
}

int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

void merge(int u, int v)
{
    int fu = find(u), fv = find(v);
    if(fu != fv)
    {
        fa[fv] = fu;
        sz[fu] += sz[fv];
        sz[fv] = 0;
    }
}

int main()
{
    int T, n, k1, k2;
    string a,b;
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &T))
    {
        while(T--)
        {
            scanf("%d", &n);
            init(2*n);
            for(int i = 0; i < n; i++)
            {
                cin >> a >> b;
                if(ma.count(a) == 0)
                {
                    k1 = ma.size();
                    ma.insert(make_pair(a,ma.size()));
                }
                else
                    k1 = ma[a];
                if(ma.count(b) == 0)
                {
                    k2 = ma.size();
                    ma.insert(make_pair(b,ma.size()));
                }
                else
                    k2 = ma[b];
                if(find(k1) != find(k2))
                        merge(k1,k2);
                cout << sz[find(k1)] << endl;
            }
        }
    }
    return 0;
}

你可能感兴趣的:(并查集,并查集)