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. missing sdkl in .NET Core 1.0.1 - VS 2015 Tooling Preview 2

    打开项目的时候,提示缺少sdk 在C:\Program Files\dotnet\sdk找不到对应的版本 解决方法: https://github.com/aspnet/Tooling/blob/ma ...

  2. Upgrade R (升级R语言)

    R R version 3.1.1 (2014-07-10) -- "Sock it to Me" yum list installed | grep R R-core.x86_6 ...

  3. Java面试宝典2015版

    这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的朋友在笔试时更好地赢得笔试和面试.由于这套面试题涉及的范围很泛,很广,很杂,大家不可能一天两天就看完 ...

  4. 巧遇"drwxr-xr-x."权限

    drwxr-xr-x. 是SELinux的问题 REDHAT6之后安全提高所以设置的 关闭SELINUX就好了 因为新版本ls把多acl和selinux属性加进去了,与系统无关,新版本的ls代码使用1 ...

  5. JAVA查找--[二分查找]

    package com.array; public class BinaryFind { /* * 项目名称:二分查找 ; * 项目要求:用JAVA对数组进行查找,并运用快速查找算法; * 作者:Se ...

  6. 《Linux内核设计的艺术》学习笔记(四)默认段和偏移寄存器

    参考书籍:<Intel微处理器> 表1 默认的16位段 + 偏移寻址组合 段 偏移量 特殊用途 CS IP 指令地址 SS SP或BP 堆栈地址 DS BX.DI.SI.8位或16位数 数 ...

  7. android tablelayout 显示图片

    当在tablelayout中显示图片时,设置imageView为固定大小时,会出现divide by zero 错误 将LayoutParams 改为 TableRow.LayoutParams即可 ...

  8. iOS - Swift NSSize 尺寸

    前言 结构体,这个结构体用来表示事物的宽度和高度. public typealias NSSize = CGSize public struct CGSize { public var width: ...

  9. amd64_or_ia64?

    amd64 网上资料: 1. IA64是intel推出的架构,AMD64是AMD推出的.IA64不兼容原有的32位x86架构指令集,后来被证明这种做法是不成功的,于是Intel发展处IA64e架构,对 ...

  10. [转载] 对象存储(2):OpenStack Swift——概念、架构与规模部署

    原文: http://www.testlab.com.cn/Index/article/id/1085.html#rd?sukey=fc78a68049a14bb228cb2742bdec2b9498 ...