Virtual Friends

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

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
 

题意:  逐步给你一些关系网,对于每一步求所给出的两个人合并之后所构成的关系网。

代码:

  #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<map>
#include<string>
using namespace std;
const int maxn=;
int father[maxn];
int rank[maxn];
//初始化
void init( int n)
{
for(int i =;i<=n ;i++){
father[i]=i;
rank[i]=;
}
}
//搜索
int fin(int x)
{
int tem=x;
while(x!=father[x]){
x=father[x];
}
//进一步压缩
while(tem!=father[tem])
{
tem=father[tem];
father[tem]=x;
} return x;
}
void Union(int a,int b){
a=fin(a);
b=fin(b);
if(a!=b){
if(rank[a]<rank[b]){
rank[b]+=rank[a];
father[a]=b;
}
else{
rank[a]+=rank[b];
father[b]=a;
}
}
}
map<string,int>sac;
char aa[maxn][],bb[maxn][]; int main()
{
int t,n;
while(scanf("%d",&t)!=EOF)
{
while(t--){
scanf("%d",&n);
if(!sac.empty()) sac.clear();
int cnt=;
for(int i=;i<n ;i++){
scanf("%s%s",aa[i],bb[i]);
// map<string,int>::iterator it;
//it=sac.find(aa[i]);
if(sac.find(aa[i])==sac.end())
sac[aa[i]]=++cnt; // it=sac.find(bb[i]);
if(sac.find(bb[i])==sac.end())
{
// posb=sac.size();
// sac.insert(pair<string,int>(bb,posb));
sac[bb[i]]=++cnt;
}
}
init(cnt);
for(int i=;i<n;i++)
{
Union(sac[aa[i]],sac[bb[i]]);
printf("%d\n",rank[fin(sac[aa[i]])]);
}
}
}
return ;
}

hdu 3172 Virtual Friends (映射并查集)的更多相关文章

  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 (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  4. hdu 3172 Virtual Friends (并查集)

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

  5. hdu 3172 Virtual Friends

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

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

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

  7. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

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

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

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. change the walltime for currently running PBS job (qalter pbs)

    qalter jobid -l walltime=X e.g.qalter 377470.manager -l walltime=2222:00:00qalter: Unauthorized Requ ...

  2. 【转】基于APD的光电探测器电路研究与设计

    光电探测器电路用于对光电转换器件输出的微弱电压或电流信号进行放大.处理和整形输出.对于不同探测用途而采用的光电转换器件不同,与之配合使用的光电探测器电路性能也因此而不同.如果用来进行光电转换,则重点考 ...

  3. [C程序设计语言]第一部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. Installing Python 3.5.2 from source

    Here are the procedures we are to follow, Download the source code of an official Python release. Co ...

  5. python_way day6 反射,正则 模块(进度条,hash)

    python_way day6 反射 正则 模块 sys,os,hashlib 一.模块: 1.sys & os: 我们在写项目的时候,经常遇到模块互相调用的情况,但是在不同的模块下我们通过什 ...

  6. onclick事件分析

     有些时候,我们想实现这样的一种效果:      <a href="imgs/2.jpg" title="A fireworks display" onc ...

  7. 51nod 1413 权势二进制 背包dp

    1413 权势二进制 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB  一个十进制整数被叫做权势二进制,当他的十进制表示的时候只由0或1组成.例如0,1,101, ...

  8. js实现checkbox的全选和全不选功能

    html代码: <form name="form1" method="post" action="manage.php?act=sub" ...

  9. 通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  10. linux之flock函数锁文件

    头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指定 ...