Kattis - Virtual Friends(并查集)
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 |
2 |
题意
朋友的朋友也是朋友,问当前的人有几个朋友
思路
并查集
代码
#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(并查集)的更多相关文章
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Kattis - flippingcards 【并查集】
题意 给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的 思路 将每一对数字 连上一条边 然后 最后 判断每一个连通块里面 边的个数 是否 大 ...
- 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 ...
- HDU 3172 Virtual Friends (map+并查集)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- HDU Virtual Friends(超级经典的带权并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- day22 包,相对/绝对路径
目录 包 包被导入时发生的三件事 为什么要有包 相对路径 绝对路径 包 包是一个文件夹,也是一个模块,只是为了区分单个文件的模块,称之为包.因为单纯的文件夹无法作为模块,文件夹内的__init__.p ...
- redis RDB快照和AOF日志持久化配置
Redis持久化配置 Redis的持久化有2种方式 1快照 2是日志 Rdb快照的配置选项: save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 ...
- Java判断字符串中是否含有英文
实现代码: /* * 判断字符串中是否含有英文,包含返回true */ public boolean isENChar(String string) { boolean flag = false; P ...
- 路飞学城Python-Day142
第2节:UA身份伪装 反爬机制 User-Agent:请求载体的身份标识 通过不同的手段的当前的请求载体是不一样的,请求信息也是不一样的,常见的请求信息都是以键和值的形式存在 浏览器的开发者工具 Ne ...
- There are no packages available for installation. Sublime3解决方法
最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报 There are no packages available for ...
- Django用户认证(四)自定义认证Customizing authentication
原文:https://www.cnblogs.com/linxiyue/p/4061044.html 扩展已有的用户模型Extending the existing User model 有两种方法来 ...
- jQuery(Dom节点操作)
- 工具-VMWARE技巧-桥接连外网-WIN7
使用虚拟机wmware如何连接宿主主机 最简单的方法,使用直接连接主机的模式,然后把宿主机的IP更改为和虚拟机同一个网段的机器就行 但是既要上网,有要连接虚拟机,就需要使用桥接,在编辑->虚拟网 ...
- BA-WG-泰豪发电机
泰豪发电机的控制主板有2个端口,一个是RS232端口,一个是RS485端口,通常接网关需要将这个RS485的端口调整为modbus协议输出,再将modbus协议通过网关转换为bacnet / ip协议 ...
- 设置utf8编码问题
注意:乱码和request的具体实现类有关,现在已经查到的是RequestDispatcher.forward调用前使用的是org.apache.catalina.connector.RequestF ...