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‘ 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

这题可以用并查集+map来做,注意可能输入的两个字符串相同。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int pre[100010],num[100010];
int find(int x)
{
	int i,j=x,r=x;
	while(r!=pre[r])r=pre[r];
	while(j!=pre[j]){
		i=pre[j];
		pre[j]=r;
		j=i;
	}
	return r;
}

int main()
{
	int T,n,m,i,j,index,a,b,t1,t2,sum;
	char s1[30],s2[30];
	while(scanf("%d",&T)!=EOF)
	{while(T--)
	{
		map<string,int>hash;
		hash.clear();
		index=1;
		scanf("%d",&n);
		for(i=1;i<=100005;i++){
			pre[i]=i;num[i]=1;
		}
		for(i=1;i<=n;i++){
			scanf("%s%s",s1,s2);
			if(hash[s1]==0)hash[s1]=index++;
			if(hash[s2]==0)hash[s2]=index++;
			t1=find(hash[s1]);
			t2=find(hash[s2]);
			if(t1==t2)printf("%d\n",num[t1]);
			else{
				pre[t1]=t2;
				num[t2]=num[t2]+num[t1];
				printf("%d\n",num[t2]);
			}
		}
	}
	}
	return 0;
}

时间: 2024-10-05 06:55:24

hdu3172 Virtual Friends的相关文章

并查集练习1

举头望明月,低头敲代码... 推荐学习地址:http://www.cnblogs.com/cyjb/p/UnionFindSets.html 简单: hdu1213 How Many Tables:新手必秒 1 #include<cstdio> 2 const int N=1001; 3 int f[N]; 4 void init(int n){ 5 for(int i=1;i<=n;++i) 6 f[i]=i; 7 } 8 int fin(int x){ 9 if(x!=f[x])f[

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 d

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