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
3
Fred Barney
Barney Betty
Betty Wilma
2
3
4

题意

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

思路

并查集

代码

#include<bits/stdc++.h>
using namespace std;
int Set[200010];
int Num[200010];
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 = 0; i <= 2 * n; ++i) {
            Set[i] = i;
            Num[i] = 1;
        }
        string A, B;
        for (int i = 0, j = 1; i < n; ++i) {
            cin >> A >> B;
            if (!Map[A]) Map[A] = j++;
            if (!Map[B]) Map[B] = j++;
            Union(Map[A], Map[B]);
        }
    }
}
时间: 2024-10-20 21:46:36

Kattis - Virtual Friends的相关文章

Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案

原文:Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案 一打开虚拟机电脑就立马蓝屏重启,新建虚拟机也没用,然后就开始百度,百度上全说什么驱动不对,然后我就卸载升级各种驱动,然后各种重装VirtualBox,还是不行,搞了一两个小时,都快放弃了.决定上个google,终于看到一篇文章说是因为Hyper-V与Virtual Box冲突了,只需要在控制面板关闭就行.然后抱着试一试的心态. 首先我们先打开控制面板.按Wi

利用letsencrypte生成证书时,create virtual environment失败

./letsencrypt-auto certonly --standalone 利用letsencrypt生成证书时 ,出现下面错误提示 0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded. Creating virtual environment... Traceback (most recent call last): File "/usr/lib/python3/dist-packages/virtualenv.py

ARM32 Linux kernel virtual address space

http://thinkiii.blogspot.jp/2014/02/arm32-linux-kernel-virtual-address-space.html The 32-bit ARM CPU can address up to 2^32 = 4GB address*. It's not big enough in present days, since the size of available DRAM on computing devices is growing fast and

How to Downgrade VMware ESXi 5.5 Virtual Machine Hardware Version to 9 from 10

Shut down the virtual machine in the vSphere client. Remove your virtual machine from the ESXi inventory. Browse your Datastore and find the virtual machine's .vmx file.   Download it to your desktop. Open the .vmx file in Notepad or your favorite te

【cl】解决Fail to create the java Virtual Machine

eclipse打开,提示Fail to create the java Virtual Machine 解决方法: 1.到eclipse安装目录下,找到eclipse.ini 2.按键盘ctrl+F,复制以下字符“launcher.XXMaxPermSize”,去掉引号,查找 3.将随后出现的两个216m改成128m

设置virtual host

在http.conf确认已载入相关模块 LoadModule vhost_alias_module modules/mod_vhost_alias.so 在httpd-vhosts.conf或者在/home/http/conf/httpd.conf 添加以下配置. <VirtualHost *:80> DocumentRoot "f:/git/minyun" ServerName minyun <Directory "f:/git/minyun"&

C++中virtual(虚函数)的用法

转载:http://blog.csdn.net/foreverhuylee/article/details/34107615 在面向对象的C++语言中,虚函数(virtual function)是一个非常重要的概念. 什么是虚函数: 虚函数是指一个类中你希望重载的成员函数 ,当你用一个  基类指针或引用   指向一个继承类对象的时候,调用一个虚函数时, 实际调用的是继承类的版本. --摘自MSDN                                                 

如何实现一个 Virtual DOM 及源码分析

Virtual DOM算法 web页面有一个对应的DOM树,在传统开发页面时,每次页面需要被更新时,都需要手动操作DOM来进行更新,但是我们知道DOM操作对性能来说是非常不友好的,会影响页面的重排,从而影响页面的性能.因此在React和VUE2.0+引入了虚拟DOM的概念,他们的原理是:把真实的DOM树转换成javascript对象树,也就是虚拟DOM,每次数据需要被更新的时候,它会生成一个新的虚拟DOM,并且和上次生成的虚拟DOM进行对比,对发生变化的数据做批量更新.---(因为操作JS对象会

Windows下Nginx Virtual Host多站点配置详解

Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 windows7配置Nginx+php+mysql教程. 先说明一下配置多站点的目的:在生产环境中,如果将系统所有代码文件都放在公开目录中,则很容易被查看到系统源码,这样是很不安全的,所以需要只公开index.php的入口文件目录.而同一个服务器中,可能运行多个系统,这样就必须公开多个入口文件目录,以便用不同的