Virtual Friends (HDU3172)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3172

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9227    Accepted Submission(s): 2669

Problem Description

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

Input file contains multiple test cases. 
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F 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
3
Fred Barney
Barney Betty
Betty Wilma

Sample Output

2
3
4

Source

University of Waterloo Local Contest 2008.09

注意此题多组数据,跟正常多的多组数据不同

AC代码:

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
const int maxn= 110000;
int pre[maxn],num[maxn];
char name1[30],name2[30];
string str1,str2;
map<string,int> mmap;
int find(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }

    return r;
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
        num[fy]+=num[fx];
        num[fx]=0;

    }
    printf("%d\n",num[fy]);   ///每次都要输出
}

int main()
{
    int t,n;
    while(~scanf("%d",&t))    ///多组数据
    {
        while(t--)
        {
            mmap.clear();
            scanf("%d",&n);
            for(int i=1; i<maxn; i++)
                pre[i]=i,num[i]=1;
            int no=1;
            while(n--)
            {
                scanf("%s%s",name1,name2);
                str1=name1;
                str2=name2;
                if(mmap.find(str1)==mmap.end()&&mmap.find(str2)!=mmap.end())
                {
                    mmap[str1]=no++;
                    join(mmap[str1],mmap[str2]);

                }
                else if(mmap.find(str2)==mmap.end()&&mmap.find(str1)!=mmap.end())
                {
                    mmap[str2]=no++;
                    join(mmap[str1],mmap[str2]);

                }
                else if(mmap.find(str2)==mmap.end()&&mmap.find(str1)==mmap.end())
                {
                    mmap[str1]=no++;
                    mmap[str2]=no++;
                    join(mmap[str1],mmap[str2]);

                }
                else if(mmap.find(str2)!=mmap.end()&&mmap.find(str1)!=mmap.end())
                {
                    join(mmap[str1],mmap[str2]);

                }

            }

        }
    }

    return 0;
}
时间: 2024-10-12 22:46:43

Virtual Friends (HDU3172)的相关文章

hdu3172 Virtual Friends

Problem Description 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' fr

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对象会