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無刷新上傳之uploadify簡單試用
先簡單的侃兩句:貌似已經有兩個月的時間沒有寫過文章了,不過仍會像以前那样每天至少有一至兩個小時是泡在园子裏看各位大神的文章.前些天在研究“ajax無刷新上傳”方面的一些插件,用SWFUpload實現了 ...
- rsync 3.1.1源代码编译安装配置
http://rsync.samba.org/ rsync-3.1.1.tar.gz ---0.卸载rpm # yum remove rsync -----1.安装: tar -zxvf rsync- ...
- hdu5375 Gray code(DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 题目大意:给你一个二进制串,带'?'的位置能够由你来决定填'1'还是'0',补充完整之后转换成 ...
- 华为云分布式数据库中间件DDM和开源MyCAT对比
前言 华为云分布式数据库中间件(Distributed Database Middleware)是解决数据库容量.性能瓶颈和分布式扩展问题的中间件服务,提供分库分表.读写分离.弹性扩容等能力,应对海量 ...
- 使用 Code Map 理解复杂代码1 ——Visual Studio2012
第一次知道code map是在Visual Studio Ultimate 2012自带的解说上面,当时认为十分好奇,所以查了查.结果一查就是好几天.原来Visual Studio Ultimate ...
- C++获取时间的方法
//方案- 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdio.h> int main( void ) { ...
- 【bzoj4538】[Hnoi2016]网络
我们考虑树剖,线段树上维护一个堆,保存不经过该段区间的路径的权值. 对于一条路径我们将对于线段树中的区间提取出来,在对于线段树中进行修改.也就是在堆中插入或删除. 对于一次询问,只要找到包含该点的线段 ...
- unigui组件中client javascript delphi组件之间的操作
UniLabel组件: function OnClick(sender, e){ MainForm.UniLabel1.setText('Click!');} function Onmousemove ...
- 今晚的两道 bc
第一道题 Beautiful Palindrome Number ,简单组合计数问题,手算打表就好~大概十五分钟左右搞定[第一次 提交竟然 wa了一次 有一个小小的坑在那.... /******** ...
- linux shell 实例1
UI项目删除“UIL”文件中的无用字串: 脚本需要制定UIL文件目录位置&无用字串的txt文件,如删除海尔目录下的无用字串: ./delete_uil_string.sh ./haier/UI ...