hdu 3172 Virtual Friends (映射并查集)
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5491 Accepted Submission(s): 1519
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.
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).
3
Fred Barney
Barney Betty
Betty Wilma
题意: 逐步给你一些关系网,对于每一步求所给出的两个人合并之后所构成的关系网。
代码:
#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 (映射并查集)的更多相关文章
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- HDU 3172 Virtual Friends (map+并查集)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3172 Virtual Friends
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
随机推荐
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 《微信开发日志》之OAuth2验证接口
OAuth2接口说明: 企业应用中的URL链接(包括自定义菜单或者消息中的链接),可以通过OAuth2.0验证接口来获取员工的身份信息. 通过此接口获取用户身份会有一定的时间开销.对于频繁获取用户身份 ...
- eclipse+tomcat开发web项目
也许正在使用的人会觉得这个过程谁不知道啊? 但是对于一个混迹各种语言编程有些年头的我来讲,却必须记录下来! 因为今天以前,我都通过配置[eclipse的tomcat插件]+编写[ant脚本,build ...
- ASP.NET四则运算--工厂模式
这次是在ASP.NET上实现四则运算,之前用策略模式实现了,所以这次想着用工厂模式实现一下. Calculator.cs using System; using System.Collections. ...
- Python学习遇到的问题
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position
- 将文件读取到内存、打印pe结构
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h&g ...
- 迷你sql profile,给缺少sql跟踪的朋友们
如果你的数据库没有sqlprofile,看这里. 如果你没时间装sqlserver那一系列的东西,看看这里,也许能解决呢. 这是一个迷你版的sqlprofile ,在win7下测试,链接sqlserv ...
- Python基础学习笔记(七)常用元组内置函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-tuples.html 3. http://www.liaoxue ...
- MyEclipse + Tomcat 热部署问题
myEclipse设置对应的tomcat时,只需要在jdk的Optional Java VM arguments中添加如下设置: -Xms256m -Xmx512m-Dcom.sun.manageme ...
- [转载] 高效MacBook工作环境配置
原文: http://www.xialeizhou.com/?p=71 高效MacBook工作环境配置 发表于 2015 年 8 月 1 日 由 xialeizhou 本文记录整个配置过程,供新入手M ...