R - Virtual Friends

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
 
 
题目大意:如果A和B是朋友,那么A的朋友圈为2,如果C和B是朋友,那么A的朋友圈为3.求朋友圈人的个数,自己也算自己的一个朋友。
 
 
#include <iostream>
#include <cstdio>
#include <map>
#include<string>
using namespace std;


const int maxn = 100000+5;


int fa[maxn];
int Rank[maxn];//并查集的节点数.Rank[i]=a表示以i为根节点的并查集的节点数是a






void make_set()
{


    for (int i = 1; i < maxn; i++)
    {                    //遍历所有的结点
        fa[i] = i;       //所有节点的父亲默认是他自己
        Rank[i] = 1;         //以i为根结点的并查集的节点数默认为1
    }
}


int find_set(int x)
 {
    if (x == fa[x])
        return x;
    return fa[x] = find_set(fa[x]);
}


void join(int x, int y)
{
    x = find_set(x);
    y = find_set(y);
    if (x != y)
    {
        fa[x] = y; //求并查集的节点数的关键代码
        Rank[y] += Rank[x];//将以x为根节点的并查集的节点数加到以y为根节点的并查集上去
        Rank[x] = 0;//以x为根节点的并查集的节点数置为0
    }
}


int main()
 {
    int T;
    while (scanf("%d", &T) != EOF)
        {
            while (T--)
            {
            make_set();
            map<string, int> m;//用于为名字结点建立一个索引,转换成数字节点
            int n,k = 1;
            scanf("%d", &n);
            while(n--)
            {
                string a,b;
                cin >> a >> b;
                if (!m[a])//(m.find(a) == m.end()) //如果map中还没有该名字
                    m[a] = k++;//则为该名字建立一个索引
                if (!m[b])//(m.find(b) == m.end())
                    m[b] = k++;
                join(m[a], m[b]);
                printf("%d\n", Rank[find_set(m[b])]);
            }
        }
    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值