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 小希的迷宫 基 ...
随机推荐
- PAT-树的同构
链接 树的同构 题意 同构的定义: 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 给两棵树,判断是否同构 做法 先建树,然后判断,知道怎么判断就知道 ...
- 【转】【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之RAC 工作原理和相关组件(三)
原文地址:http://www.cnblogs.com/baiboy/p/orc3.html 阅读目录 目录 RAC 工作原理和相关组件 ClusterWare 架构 RAC 软件结构 集群注册(OC ...
- bzoj 1191: [HNOI2006]超级英雄Hero 网络流_残量网络
题目描述: 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的 多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一道题后 ...
- Python基础学习_01字符串的拼接(字符串的格式化输出)
# 字符串的拼接 ---字符串的格式化输出 # 字符串的拼接 ---字符串的格式化输出 name = input("name:") age = input("age:&q ...
- alg--动态规划(dynamic planning)
怎么开头呢? 一句话概括吧, dp的思想就是递归的反思想. 参考的理化: https://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741374.h ...
- Java开源框架 iBase4J 搭建笔记
项目地址:https://gitee.com/iBase4J/iBase4J 搭建步骤: 1.git 代码 2.安装 Zookeeper 3.打包部署 Dubbo Admin ...
- BindingResult不能获取错误对象
BindingResult不能获取错误对象,代码如下: @RequestMapping(value = "/login") public String error4( Model ...
- Spring Boot 第一个demo
Sring boot 一直没有使用过,跳槽来到新公司,暂时没有事情就学习一下. Spring boot 这里采用的是maven 来创建的 maven项目的pom.xml 文件 <?xml v ...
- tomcat work目录的作用就是编译每个项目里的jsp文件为java文件如果项目没有jsp页面则这个项目文件夹为空
最近发现,很多网友喜欢把tomcat的work目录里的东西叫做缓存,其实那不是很恰当,work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录,这也正是为什么它 ...
- 设计模式之二十:责任链模式(Chain of Responsibility)
感觉这个设计模式和组合模式一样是一种非常巧妙的设计模式,在须要使用它的地方假设不使用这样的设计模式代码会变的非常复杂,可是这样的设计模式的基本原理又是非常easy的. 责任链模式: 通过使多个对象都有 ...