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. CodeForces 567C Geometric Progression

    Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  2. 【转】VB中应用DDE

    动态数据交换(dde)是windows应用程序间通讯的基本方法之一,在动态数据交换的过程中,提供数据和服务的应用程序称为服务器,请求数据或服务的应用程序则称为客户. dde交谈是由客户程序启动的.如果 ...

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

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

  4. poj 1502 最短路+坑爹题意

    链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...

  6. Android——学习:线性布局权重分配

    LinearLayout在Android中被广泛使用,LinearLayout有一个比较重要的属性——android:layout_weight.按照字面理解就是该控件的权重,这个值默认是 零(0). ...

  7. Redis数据导入工具优化过程总结

    Redis数据导入工具优化过程总结 背景 使用C++开发了一个Redis数据导入工具 从oracle中将所有表数据导入到redis中: 不是单纯的数据导入,每条oracle中的原有记录,需要经过业务逻 ...

  8. objc_msgSend(): Too many arguments to function call ,expected 0,have3

    runtime 使用的时候,需要设置一下: Build Setting--> Apple LLVM 6.0 - Preprocessing--> Enable Strict Checkin ...

  9. 虚拟机安装Centos64位Basic Service后 ifconfig查看无ip

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ONBOOT="no" 改为 ONBOOT="yes" 保存后: ...

  10. WCF学习资源收集汇总

    1.WCF编程 http://www.cnblogs.com/wengyuli/category/217446.html 2.wcf热门问题编程示例 http://blog.csdn.net/book ...