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. MySQL安装失败解决的方法

    一..msi版的MySQL安装包在最后执行的时候到第三步就死掉了,直接未响应 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd19iYXNrZXRib3ky ...

  2. 20170719 Mysql 配置远端Mysql访问,增加表/存储过程

    -- 1 .在windows 环境中安装Mysql 会按照到默认的C盘当中,如何修改呢--? -- 2. 如何只安装客户端不安装Mysql 数据库服务  --? -- 3. 表的特殊列,默认采用函数值 ...

  3. 数据库---初识sql语句

    初识sql语句 SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发.SQL语言分为3种类型: DDL语句     数据库定义语言: 数据库.表.视图.索引.存储 ...

  4. golang fmt格式“占位符”

    # 定义示例类型和变量 type Human struct { Name string } var people = Human{Name:"zhangsan"} 普通占位符 占位 ...

  5. 查看npm仓库版本号

    http://repo.inspur.com:8081/artifactory/webapp/#/artifacts/browse/simple/General/thirdparty iop 找到现在 ...

  6. Linux更改IP地址

    1.进入到root用户 2.执行命令:ifconfig 查看本机的名称 3.执行命令:ifconfig eth0 192.168.25.128 netmask 255.255.255.0  //eth ...

  7. 单例模式多线程安全写法(double-lock-check)

    原始版本 public static Object getInstance() { if (instance != null) { return instance; } instance = new ...

  8. js模拟栈---汉诺塔

    var Stack = (function(){ var items = new WeakMap(); //先入后出,后入先出 class Stack{ constructor(){ items.se ...

  9. Java Selenium - 元素操作 (四)

    四,弹出框 京东购物车为例 , 点击‘删除’ 或者‘移到我的关注’ ,就会弹出下面这个框框,练吧: (其实这也不是常规的弹出框,二是div css前端技术做的效果,本想做个Alert的案例,实在不好找 ...

  10. jquery简介未完成

    jQuery jQuery是一个快速.简洁的javascript框架,是继prototype之后又一个优秀的代码库(或者javascript框架).jQuery设计的宗旨是writeLess,Do M ...