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. javaee的toString的用法

    toString方法返回该对象的字符串表示,如果不用toString则返回的是地址 package Xuexi; public class Person { private String name; ...

  2. jsmind实现思维导图,和echars 的tree图类似

    https://blog.csdn.net/qq_41619796/article/details/88552029

  3. vue自己写了一个div菜单,点击按钮展开,点击其他地方关闭这个div菜单

    需求是通过点击body页面,在其他地方就关闭这个<div>菜单,给这个div一个id:problemList,但是点击我打开的按钮,不关闭. created () { document.o ...

  4. 莫烦大大TensorFlow学习笔记(9)----可视化

      一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...

  5. 实验一:JAVA实验环境搭建 ,JDK下载与安装及 Eclipse下载与安装

    一.搭建JAVA实验环境 1.JDK的下载 (1)打开 IE 浏览器,输入网址“http://www.oracle.com/index.html”,浏览 Oracle 官方主页.鼠标双击Downloa ...

  6. Bash中文速查表

    最好用的中文速查表(Cheatsheet) 来源:https://github.com/skywind3000/awesome-cheatsheets 感谢网友们的贡献! ############## ...

  7. [luogu2680] 运输计划 (lca+二分+树上差分)

    传送门 Description Input Output 一个整数,表示小 P 的物流公司完成阶段性工作所需要的最短时间. Sample Input 6 3 1 2 3 1 6 4 3 1 7 4 3 ...

  8. Linux操作随笔

    1.查看php加载的模块 /usr/local/php/bin/php -m |less 2.查询连接数 netstat -ntu | awk '{print $5}' | cut -d: -f1 | ...

  9. Linux 中常用的基础命令二

    1.Linux文件分层结构  FHS:Linux有一个组织叫LSB定义的Linux发行版基础目录名称命名法则及功用规定,这种标准叫FHS文件系统层级标准./bin   存放可执行的二进制程序,管理员和 ...

  10. Linux 文件压缩

    压缩工具   compress/uncompress:对应 .Z 结尾的压缩格式文件  压缩格式:gz.bz2.xz.zip.Z gzip  压缩文件并删除源文件(生成.gz的文件) gunzip 解 ...