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

InputcopyOutputcopy
1 3
Fred Barney
Barney Betty
Betty Wilma 
2
3
4

思路就是map加并查集, 这个题目是我第一次用map, 所以说浅谈一下map的作用, map第一个元素是关键字, 在map中只能出现一次, 然后第二个元素可以理解为是关键字的值,map內部的实现自建一颗红黑树, 本题中的string和int存在一对一的映射关系。

所以说回归到本题, map负责判断是否出现过并且用第二个元素给他下标, 带权并查集实现的操作是合并, 也就是说把一个小的祖宗认另外一个大的祖宗当爹

if (a !=  b) { // 一个大的
            f[b] = a; // 认爹
            cnt[a] += cnt[b]; //把自己的儿子给爹
            cout << cnt[a] << endl; //输出祖宗一共多少儿子(包括自己

这个题目我写的时候输入卡了好多好多发, 记得while(cin >> t)欲哭无泪!!!!!!改了十多次了唉, 脑瘫。

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int N = 2e06 + 10;
int f[N], cnt[N];

void init() {
    for (int i = 0; i <= N; i++) {
        f[i] = i;
        cnt[i] = 1;
    }
}

int find(int x) {
    if (x != f[x]) f[x] = find(f[x]);
    return f[x];
}

void mem(int a, int b) {
    a = find(a);
    b = find(b);

    if (a == b) {
        cout << cnt[a] << endl;
    } else {
        f[b] = a;
        cnt[a] += cnt[b];
        cout << cnt[a] << endl;
    }
}

int main() {
    map<string, int> q;
    int t;
    while (cin >> t) {
        while (t--) {
            init();
            q.clear();
            string a, b;
            int n, num = 1;
            cin >> n;
            for (int i = 0; i < n; i++) {
                cin >> a >> b;
                if (q[a] == 0) q[a] = num++;
                if (q[b] == 0) q[b] = num++;
                mem(q[a], q[b]);
            }
        }
    }
    return 0;
}

最后

祝各位安好, 愿acm之路一路顺风!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值