HDU Virtual Friends(超级经典的带权并查集)
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11092 Accepted Submission(s): 3221
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
3
4
题目说的是,网上交友,朋友的朋友也是自己的朋友,问每次两个交了朋友之后,他们朋友圈里有多少人,
并查集处理比较顺,用上map 编号,比较简单就处理了.....但是这个题目的输入比较坑,很少见到这样的输入..........无限次指定次数的输入.........
分析:
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<string.h>
#include<memory>
using namespace std;
#define max_v 100005
#define INF 9999999
int pa[max_v+];
int num[max_v+];
int n;
void init()
{
for(int i=; i<max_v; i++)
{
pa[i]=i;
num[i]=;//权
}
}
int find_set(int x)
{
if(pa[x]!=x)
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
int fx=find_set(x);
int fy=find_set(y); if(fx!=fy)
{
pa[fx]=fy;
num[fy]+=num[fx];//!!!计数
}
printf("%d\n",num[fy]);
}
int main()
{
int t;
while(~ scanf("%d",&t))
{
string str1,str2;
while(t--)
{
init();
map<string,int>mm;
scanf("%d",&n);
int k=;
for(int i=; i<n; i++)
{
cin>>str1>>str2;
if(!mm[str1])
{
mm[str1]=k++;
}
if(!mm[str2])
{
mm[str2]=k++;
}
union_set(mm[str1],mm[str2]);
}
}
}
return ;
}
HDU Virtual Friends(超级经典的带权并查集)的更多相关文章
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- Travel(HDU 5441 2015长春区域赛 带权并查集)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 洛谷 1196 [NOI2002]银河英雄传说【模板】带权并查集
[题解] 经典的带权并查集题目. 设cnt[i]表示i前面的点的数量,siz[i]表示第i个点(这个点是代表元)所处的联通块的大小:合并的时候更新siz.旧的代表元的cnt,路径压缩的时候维护cnt即 ...
- 并查集例题02.带权并查集(poj1182)
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
随机推荐
- 精选10款HTML5手机模板
1.Stroller | Mobile & Tablet Responsive Template 演示地址 购买地址 2.Ocean Mobile Template 演示地址 购买地址 ...
- <!DOCTYPE> 标签是什么
DOCTYPE 标签,是html文档的类型声明(document type declaration,所谓声明,也就是宣称我他妈是谁),用来告诉浏览器,使用什么样的文档类型定义(Document Typ ...
- Web开发须知的浏览器内幕 缓存与存储篇(2)
本文禁止转载,由UC浏览器内部出品. 3. HTTP Cache 综述 HTTP Cache是完全按照IETF规范实现的,最新的RFC规范地址是 https://tools.ietf.org/html ...
- 图片裁剪(基于RxPaparazzo)
图片裁剪(基于RxPaparazzo) 前言:基于RxPaparazzo的图片裁剪,图片旋转.比例放大|缩小. 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 涉及 ...
- 一次线上bug引起的反思
今天线上又出现了一个bug,而且代码是我写的.之前这个问题也出现过,不过由于每次情况都不同,改来改去总是改不完.之后领导知道后也很恼火,让测试把每种情况都测试了下,而我也又一次重新检查了下代码.当时确 ...
- linux系统PKWindows系统,从各方便分析linux和Windows的优劣
服务器系统linux系统和linux系统哪个好用,公说公有理婆说婆有理,今天鼎峰凡凡大概对Linux系统与Windows系统的优缺点PK!可以从以下几个方面来看 ①成本 赞成Linux的声音Linu ...
- 卸载Sharepoint2016后。重新安装提示 系统从以前的安装重新启动,或更新正在等待错误
卸载Sharepoint2016 重启N遍,不停地重启.需要删除注册表项 下的 .将PendingFileRenameOperations键项删除,再重新安装就可以安装成功.
- 将旧的时间字符串转换为新的string时间字符串
旧的时间字符串-->simpledataformat1.parse(该字符串) 获得date类型 -->simpledataformat2.format(date) simpledatef ...
- TMG 2010 为HTTPS协议添加非标准端口(443)
1.添加加密端口时,编辑脚本addsslports.vbs addsslports.vbs 脚本内容如下: Dim root Dim tpRanges Dim newRange Set root = ...
- 3.5星|《刷屏:视频时代的风传法则》:YouTube热门视频回顾与分析
刷屏:视频时代的疯传法则 作者2010年加入YouTube.本书是作者对YouTube上的热门视频的回顾与分析.第2-9章各讲一个类别的热门视频:恶搞.混音.网络歌曲.广告.现场目击.知识与科技.小众 ...