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. eclipse+tomcat开发web项目

    也许正在使用的人会觉得这个过程谁不知道啊? 但是对于一个混迹各种语言编程有些年头的我来讲,却必须记录下来! 因为今天以前,我都通过配置[eclipse的tomcat插件]+编写[ant脚本,build ...

  2. [HIHO119]网络流五·最大权闭合子图(最大流)

    题目链接:http://hihocoder.com/contest/hiho119/problem/1 题意:中文题意. 由于1≤N≤200,1≤M≤200.最极端情况就是中间所有边都是满的,一共有N ...

  3. jdk安装环境变量设置

    设置JAVA_HOME,点击新建,变量名:JAVA_HOME,变量值:D:\java\jdk1.7.0,即刚才jdk安装的路径.   设置CLASSPATH属性,变量名:CLASSPATH,变量值:. ...

  4. NSValue

    1.利用NSValue包装自定义的结构体    typedef struct{        int age;        char *name;        double height;    ...

  5. 没有Path的Binding

    当Binding源本身就是数据且不需要Path来指明时,可以设置Path的值为".",或直接省略Path.XAML中这个"."可以省略不写,但在C#代码中是不能 ...

  6. [转载] C++ 程序员快过来围观:非常实用全面的 C++ 资源

    原文: http://codecloud.net/c-plus-plus-resource-2983.html 绝对是c++开发者的福音啊, 必须推荐. 这次的资源涉及到了标准库.Web应用框架.人工 ...

  7. 10 database tables

    本章提要-----------------------------------各种数据库表的讨论, 并介绍什么情况使用哪种表情调表的物理存储特征---------------------------- ...

  8. poj3565Ants(KM-几何与图论的结合)

    链接 可以看出蓝的之和一定比红的之和要大,也就是说符合条件的匹配一定是权值最小的,所以二分图的最佳完美匹配..KM #include <iostream> #include<cstd ...

  9. 半平面交模板(O(n*n)&& O(n*log(n))

    摘自http://blog.csdn.net/accry/article/details/6070621 首先解决问题:什么是半平面? 顾名思义,半平面就是指平面的一半,我们知道,一条直线可以将平面分 ...

  10. wpf datagrid 如何让标头 及内容居中

    头就是这么居中<DataGrid> <DataGrid.ColumnHeaderStyle> <Style TargetType="DataGridColumn ...