HDU_3172_带权并查集
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8229 Accepted Submission(s): 2363
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<iostream>
- #include<cstdio>
- #include<map>
- #include<string>
- using namespace std;
- #define N 200005
- map<string,int>m;
- int father[N],dist[N];
- int Find(int x)
- {
- if(father[x]!=x)
- father[x]=Find(father[x]);
- return father[x];
- }
- void Merge(int a,int b)
- {
- int fa=Find(a);
- int fb=Find(b);
- if(fa!=fb)
- {
- father[fa]=fb;
- dist[fb]+=dist[fa];
- }
- printf("%d\n",dist[fb]);
- }
- int main()
- {
- int t;
- while(scanf("%d",&t)!=EOF)
- {
- while(t--)
- {
- m.clear();
- int n,cnt=;
- string name1,name2;
- scanf("%d",&n);
- for(int i=; i<=*n; i++)
- {
- father[i]=i;
- dist[i]=;
- }
- while(n--)
- {
- cin>>name1>>name2;
- if(m.find(name1)==m.end())
- m[name1]=cnt++;
- if(m.find(name2)==m.end())
- m[name2]=cnt++;
- Merge(m[name1],m[name2]);
- }
- }
- }
- return ;
- }
HDU_3172_带权并查集的更多相关文章
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
随机推荐
- jquery 联动 年月日
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JQuery实例 - 生成年 ...
- 【Unix编程】进程间通信(IPC)
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息.IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket. ...
- HDMI接口基础知识及硬件设计
参考资料:http://blog.csdn.net/u013625961/article/details/53434189: http://blog.csdn.net/u014276460/artic ...
- R语言pdf输出中文乱码处理
1.使用基础包.使用函数pdf()输出 在使用pdf()函数时,要输出中文,仅仅有一种字体可选. 样例: pdf("chinese.pdf",family="GB1&qu ...
- [转] CVonline: Image Databases
转自:CVonline by Robert Fisher 图像数据库 Index by Topic Action Databases Biological/Medical Face Databases ...
- nyoj 739 笨蛋的难题四
笨蛋难题四 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 这些日子笨蛋一直研究股票,经过调研.最终发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...
- Explicit Interface Implementation (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173157.aspx If a class implements two interfaces that con ...
- ASP.NET调用存储过程并接收存储过程返回值
ASP.NET调用存储过程并接收存储过程返回值 2010-08-02 11:26:17| 分类: C#|字号 订阅 2010年02月27日 星期六 23:52 假设表结构Create T ...
- HTTP权威指南阅读记录 - 第一章
最近终于开始看<HTTP权威指南>了,第一章主要是简介一些基本的概念.下面列出一些常用,但还不是很了解的简单概念. 一.常见概念: 1.媒体类型 因特网上有数千种不同的数据类型,HTTP仔 ...
- [luogu2620]虫洞
https://www.zybuluo.com/ysner/note/1284536 题面 给一个一维坐标系,出发点为\(0\),目标点为\(w\). 每\(1\)秒可以往后移不超过\(s\)个单位距 ...