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. 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172
 
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.

InputInput 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).OutputWhenever 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 题解:带权并查集 可以记录每个集合中元素的个数
      普通并查集中增加sum数组在记录每个父节点的深度(即该集合中元素个数)
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=;
const int mod=1e9+;
map<string,int> mp;
int sum[N],f[N];
void init()
{
for(int i=;i<N;i++)
f[i]=i,sum[i]=;
}
int Find(int x)
{
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int Union(int a,int b)
{
int p=Find(a);
int q=Find(b);
if(p!=q){
f[p]=q;
sum[q]+=sum[p];
}
return sum[q];
}
int main()
{
int t,n;
while(scanf("%d",&t)!=EOF){
while(t--){
mp.clear();
scanf("%d",&n);
init();
int num=;
char a[],b[];
while(n--){
int u,v;
scanf("%s%s",a,b);
if(mp[a]==) mp[a]=num++;
if(mp[b]==) mp[b]=num++;
u=mp[a],v=mp[b];
int k=Union(u,v);
printf("%d\n",k);
}
}
}
return ;
}

HDU 3172 Virtual Friends (map+并查集)的更多相关文章

  1. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  2. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  3. hdu 3172 Virtual Friends (映射并查集)

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

  4. hdu 3172 Virtual Friends (并查集)

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

  5. HDU 2419 Boring Game(并查集+map)

    感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...

  6. hdu 3172 Virtual Friends

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...

  7. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  8. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  9. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

随机推荐

  1. UIBezierPath使用

    效果图,Demo的例子是我自己做的,下面曲线的代码是从别处copy过来的 copy地址 -(void)touchesBegan:(NSSet<UITouch *> *)touches wi ...

  2. SSIS--(1)

    目标:两组数据比对,A 来源Excel ,B 来源 Sql server DB ,比对合并,取值放入目标 C 中 首先使用工具SSIS包 一,以数据源 A 为准核对B 中是否有A 的数据和计算等动作 ...

  3. Python3学习之路~4.2 迭代器

    可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的generator fun ...

  4. 001-快速搭建Spring web应用【springboot 2.0.4】-gradle、springboot的启动过程分析、gradle多模块构建

    一.概述 学习<精通Spring MVC4>书籍笔记 二.笔记 1.快速构建Spring starter web项目几种方式 1>使用Spring Tool Suite生成Start ...

  5. 20165236 2017-2018-2 《Java程序设计》第八周学习总结

    20165236 2017-2018-2 <Java程序设计>第八周学习总结 一.第十二章教材内容总结: 1.Java的多线程机制: 多线程是指一个应用程序中同时存在几个执行体,按几条不同 ...

  6. springmvc aop 事务配置

    对应的中文: 任意公共方法的执行: execution(public * *(..)) 任何一个以“set”开始的方法的执行: execution(* set*(..)) AccountService ...

  7. 实习培训——Java异常处理(8)

    实习培训——Java异常处理(8) Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 j ...

  8. (转)EOSIO开发(四)- nodeos、keosd与cleos

    前一篇文章介绍了EOSIO中钱包.账户与账户权限的概念,这一篇文章继续学习EOSIO系统的主要组件,包括nodeos.keosd以及cleos. 本文执行的命令都是基于Docker环境,请先下载Doc ...

  9. Python 全栈开发二 python基础 字符串 字典 集合

    一.字符串 1,在python中,字符串是最为常见的数据类型,一般情况下用引号来创建字符串. >>ch = "wallace" >>ch1 = 'walla ...

  10. js抽红包分配

    将 50000元随机分给10个人,其中3个人必须分到百位数,4个人分到千位数,3个人分到万位数,每个人所得金额 <!DOCTYPE html> <html lang="zh ...