HDU 3172 Virtual Friends(map+并查集)
Virtual Friends
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 61 Accepted Submission(s) : 28
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
1
3
Fred Barney
Barney Betty
Betty Wilma
Sample Output
2
3
4
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
using namespace std;
int tot,T,n;
int num[],team[];
map<string,int >mp;
int name(char *ch)
{
if (mp.find(ch)==mp.end())
{
mp[ch]=++tot;
return tot;
}
else return mp[ch];
}
int findteam(int k)
{
if (team[k]==k) return k;
else return team[k]=findteam(team[k]);
}
int main()
{
while(~scanf("%d",&T))
{
for(;T>;T--)
{
scanf("%d",&n);
tot=; mp.clear();
for(int i=;i<=;i++)
{
team[i]=i;
num[i]=;
}
for(int i=;i<=n;i++)
{
char str1[],str2[];
scanf("%s%s",&str1,&str2);
int id1=name(str1);
int id2=name(str2);
int fx=findteam(id1);
int fy=findteam(id2);
if (fx==fy) printf("%d\n",num[fx]);
else
{
team[fx]=fy;
num[fy]+=num[fx];
printf("%d\n",num[fy]);
}
}
}
}
return ;
}
HDU 3172 Virtual Friends(map+并查集)的更多相关文章
- 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 (映射并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 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 2419 Boring Game(并查集+map)
感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...
- 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 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
随机推荐
- $一步一步学Matlab(4)——使用Matlab进行初等数学运算
Matlab可以看成是一个功能强大的计算器,那么既然是计算器,进行基本的数学运算绝对是必不可少的.本文主要讲解如何用Matlab做初等数学运算,所谓"初等数学运算",可以理解成是小 ...
- linux命令——chmod/chown
改变文件所有权chown 例如 sudo chown username myfile 1 myfile文件的所有权变为username. chown -R username /files/work 1 ...
- DbEntry 4.2 建立关系时的一些问题
创建关系提示输入字符串的格式不正确 使用HasMany Attribute时,需要将属性访问器的set部分修改为private,否则会提示输入字符串的格式不正确 [HasMany(OrderBy = ...
- Python3.x:报错POST data should be bytes, an iterable of bytes
Python3.x:报错POST data should be bytes, an iterable of bytes 问题: python3.x:报错 POST data should be byt ...
- 基于Node.js的爬虫工具 – Node Crawler
Node Crawler的目标是成为最好的node.js爬虫工具,目前已经停止维护. 我们来抓取光合新知博客tech栏目中的文章信息.访问http://dev.guanghe.tv/category/ ...
- kali 2.0下搭建DVWA环境
DVWA (Dam Vulnerable Web Application)DVWA是用PHP+Mysql编写的一套用于常规WEB漏洞教学和检测的WEB脆弱性测试程序.包含了SQL注入.XSS.盲注等常 ...
- 20145321 《Java程序设计》第10周学习总结
20145321 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程:网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. IP地址:为了能够方便的识别网络上 ...
- 20145231熊梓宏 《网络对抗》 实验8 Web基础
20145231熊梓宏 <网络对抗> 实验8 Web基础 基础问题回答 ●什么是表单? 表单是一个包含表单元素的区域,表单元素是允许用户在表单中输入信息的元素,表单在网页中主要负责数据采集 ...
- SPOJ8222 NSUBSTR - Substrings
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- [转载]Eclipse的常用快捷键
常用的快捷键 ctrl+1:快速修复错误 ctrl+shift+L :查看快捷键 alt+?或alt+/:自动补全代码或者提示代码 ctrl+o:快速outline视图 ctrl+shift+r:打开 ...