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 |
---|---|
|
|
题意
朋友的朋友也是朋友,问当前的人有几个朋友
思路
并查集
代码
- #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 小希的迷宫 基 ...
随机推荐
- bzoj4282 慎二的随机数列 树状数组求LIS + 构造
首先,我们不难发现N个位置都选一定不会比少选任意几个差,所以我们就先设定我们将这N个修改机会都用上, 那么如果点 i">ii 前有sumv">sumvsumv个可修改点 ...
- 原生js模拟双色球
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- nyoj28-大数阶乘
大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数m(0& ...
- nyoj2-吝啬的国度
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...
- mplayer configure选项中文注释
mplayer configure选项中文注释(MPlayer-1.0rc2) http://blogold.chinaunix.net/u3/104581/showart_2322466.html ...
- @Bean 指定初始化和销毁方法
bean 的生命周期 bean 的创建 --> 初始化 --> 销毁 ioc 容器管理 bean 的声明周期 可以自定义初始化和销毁方法 构造器( 对象创建 )被调用时机 单实例:在容器启 ...
- 流媒体播放器VLC SDL
http://www.cnblogs.com/lihuixian001/archive/2013/03/15/2957103.html https://wiki.videolan.org/Win32C ...
- mongodb--linux下的安装
linux下ubuntu的安装及启动 sudo apt-get upgrade sudo apt-get udpate sudo apt-get install mongodb 用 命令查看一下mon ...
- 工具-docker01
- iis配置问题报错 -- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies
具体提示: Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. 试图加载格式不正确的程序. ...