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

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer FF, the number of friendships formed, which is no more than 100000100000. Each of the following FF 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 Sample Output 1
1
3
Fred Barney
Barney Betty
Betty Wilma
2
3
4

题意

朋友的朋友也是朋友,问当前的人有几个朋友

思路

并查集

代码

#include<bits/stdc++.h>
using namespace std;
int Set[];
int Num[];
int Findroot(int x) {
if(x = Set[x])
return x;
return Set[x] = Findroot(Set[x]);
}
void Union(int x, int y) {
x = Findroot(x);
y = Findroot(y);
if(x != y) {
Set[x] = y;
Num[y] += Num[x];
}
cout << Num[y] << endl;
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
map<string, int> Map;
for (int i = ; i <= * n; ++i) {
Set[i] = i;
Num[i] = ;
}
string A, B;
for (int i = , j = ; i < n; ++i) {
cin >> A >> B;
if (!Map[A]) Map[A] = j++;
if (!Map[B]) Map[B] = j++;
Union(Map[A], Map[B]);
}
}
}

Kattis - Virtual Friends(并查集)的更多相关文章

  1. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. Kattis - flippingcards 【并查集】

    题意 给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的 思路 将每一对数字 连上一条边 然后 最后 判断每一个连通块里面 边的个数 是否 大 ...

  3. D - Association for Control Over Minds Kattis - control (并查集+STL)

    You are the boss of ACM (Association for Control over Minds), an upstanding company with a single go ...

  4. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  5. HDU Virtual Friends(超级经典的带权并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

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

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  7. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  8. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. 【转】【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之缓存融合技术和主要后台进程(四)

    原文地址:http://www.cnblogs.com/baiboy/p/orc4.html   阅读目录 目录 Cache Fusion 原理 什么是 Cache Fusion? 什么是高可用 FA ...

  2. [POI2005]AUT-The Bus 树状数组维护最大前缀和

    #include<cstdio> #include<algorithm> using namespace std; const int N=100000+3; int x[N] ...

  3. nginx全局查看进程

    1. 查看nginx的PID,以常用的80端口为例: [root@rbtnode1 nginx-1.14.2]# netstat -anop | grep 0.0.0.0:80tcp 0 0 0.0. ...

  4. pyhton的selenium的搭建

    一.好记性不如烂笔头,小伙伴们.让我们做下笔记吧 1.首先要安装pycharm  激活注册码地址:http://idea.lanyus.com/ 2.下载python3.6   python下载地址: ...

  5. mysql 与 memcache 字段名后面有空格时会产生什么问题(转)

    同事下午遇到一问题,MySQL 和 Memcached 对于同一个key,不能对应起来.最终原因是:PHP将key写入MySQL数据库之前,没有经过trim()过滤首尾空格(关键是尾部空格),结果: ...

  6. N1-1 - 树 - Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...

  7. 2016 年 Java 工具和技术的调查:IDEA 已超过

    最近「技术最前线」看到 RebelLabs 做了一次 2016 年 Java 工具与技术的调查,调查报告虽然是 6 月公布的,但数据一点也不过时. 所以「技术最前线」忙会了一中午,写了这篇文章,带大家 ...

  8. yii 表单小部件使用

    首先创建model层 因为要使用表单小部件 所以要加载相应的组件 这里需要的组件有 yii\widgets\ActiveForm 和 yii\helpers\Html 接下来在model定义的clas ...

  9. POJ 2019

    简单的RMQ,可我怎么写都WA.不明白,找了一个和我相似的贴过了,要赶着去外婆家. #include <iostream> #include <algorithm> #incl ...

  10. SVN文件恢复

    SVN删除文件 一.本地删除 SVN删除文件里的本地删除,指的是在clientdelete了一个文件,但还没有commit.使用revert来撤销删除. 二.server删除 1.通过本地删除后提交s ...