Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9179 Accepted Submission(s): 2654

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


解题心得

  • 这个是一个带权并查集,其实也没有什么新奇的,主要就是需要记录每一个string的根,这个可以用一个map来映射一下,将每一个string映射成一个数字,然后用映射的数字来进行合并就行了。
  • 这个题需要输出的东西是当前建立关系的网络里面的人数,所以需要用数组来记录一下当前根的人数就可以了。
  • 其实这个题主要处理的就是一个关于string的问题,当需要将一个string用一个数子来代替的时候可以很流畅的想到map 的映射,具体的映射操作看代码。

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+10;
int father[maxn*2],ans,num[maxn*2];
map <string,int> mp;//string映射成一个number int Find(int x)
{
if(father[x] == x)
return x;
else
return father[x] = Find(father[x]);
} void _merge(int a,int b)
{
int fa = Find(a);
int fb = Find(b);
if(fa != fb)
{
father[fa] = fb;
num[fb] += num[fa];//合并的时候要将不同根的网络的人数也合并起来
}
printf("%d\n",num[fb]);
} int main()
{
int t;
while(scanf("%d",&t) != EOF)
{
while(t--)
{
int n;
mp.clear();
ans = 1;
scanf("%d",&n);
int cnt = 0;
for(int i=0; i<=n; i++)
{
father[i] = i;
num[i] = 1;
}
for(int i=0; i<n; i++)
{
string s1,s2;
cin>>s1>>s2;
//将string用一个数字来表示
if(mp.find(s1) == mp.end())
mp[s1] = cnt++;
if(mp.find(s2) == mp.end())
mp[s2] = cnt++; _merge(mp[s1],mp[s2]);
}
}
}
}

带权并查集:HDU3172-Virtual Friends的更多相关文章

  1. HDU_3172_带权并查集

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  3. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  4. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  5. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  6. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  7. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  8. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  9. poj1984 带权并查集

    题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...

随机推荐

  1. android模拟器创建时的PANIC: Could not open:错误的解决

    创建AVD之后,在启动时报如下错误,解决方法如下: 在环境变量中创建ANDROID_SDK_HOME=D:\Program Files (x86)\Android\android-sdk,后面的当然是 ...

  2. 微服务实践分享(2)api网关

    1.作用[http://chuansong.me/n/465796751848]: 一个完整的.「面向接入」的API GW需要包含以下功能: 面向运行期 对客户端实现身份认证 通信会话的秘钥协商,报文 ...

  3. angular2 基于webpack环境搭建

    目录结构: angular-quickstart |_ ts |_ app.ts |_ index.ts |_ index.html |_ package.json |_ tsconfig.json ...

  4. springboot 学习笔记(三)

    (三)用jar包启动springboot项目 1.首先需要在pom文件中添加依赖,spring-boot-starter-parent包含有打包的默认配置,如果要修改的话要可以进行重新定义,具体内容参 ...

  5. Windows Azure 配置Active Directory 主机(1)

    现在越来越多企业将自己业务系统迁移云端,方便公司日常运维管理.这篇文章将简单介绍一下,从 Windows Azure 虚拟网络上的虚拟机 (VM) 中的 Corp Active Directory 林 ...

  6. POJ 1651 Multiplication Puzzle (区间DP,经典)

    题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...

  7. HTTP协议初探

    HTTP协议初探 HTTP协议初探 什么是http协议? 遵守协议的双方 再来回答什么是http协议 抓到这两段文本 可以总结出以下规律 HTTP 请求命令(动作,谓词 ,METHOD) GET 和 ...

  8. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

  9. guruguru

    6576: guruguru 时间限制: 1 Sec  内存限制: 128 MB提交: 28  解决: 12[提交] [状态] [讨论版] [命题人:admin] 题目描述 Snuke is buyi ...

  10. C++ Stack 与String

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" ...