hdu Virtual Friends
这题是一个很简单额并查集的题目,首先第一步是要用map将字符串映射为整型,这样方便后面的处理,然后就是用一个rank[]数组来记录每个朋友圈的人数。之后就是简单的并查集操作了。
这里给出一组测试案例:
1
6
Fred Barney
Barney Betty
Betty Wilma
AAAAA BBBBB
AAAA BBBBB
AAAA Fred
3
4
2
3
7
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string"
#include"string.h"
#include"cmath"
#include"ctype.h"
#include"map"
#include"vector"
#include"queue"
#include"stack"
using namespace std;
const int mx=;
int num_nest;
int F;
map<string,int>name;
int fa[*mx];
int Rank[*mx]; void Set()
{
for(int i=;i<*F;i++)
{
fa[i]=i;
Rank[i]=;
}
} int Find(int x)
{
int t1,t2=x;
while(t2!=fa[t2]) t2=fa[t2];
while(x!=t2)//可以减小时间复杂度
{
t1=fa[x];
fa[x]=t2;
x=t1;
}
return t2;
} void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
fa[fx]=fy;
Rank[fy]+=Rank[fx];
Rank[fx]=Rank[fy]; }
} bool is_exist(const string &str)//查找map中是否已存在该元素
{
return name.find(str)== name.end();
} void IO()
{
while(scanf("%d",&num_nest)==)
{
while(num_nest--)
{
string name1,name2;
int cnt=,id1,id2;
name.clear();
scanf("%d",&F);
Set();
getchar();
while(F--)
{
cin>>name1;
if(is_exist(name1))
{
name.insert(pair<string,int>(name1,cnt));
id1=cnt;
cnt++;
}
else
id1=(name.find(name1))->second;
cin>>name2;
if(is_exist(name2))
{
name.insert(pair<string,int>(name2,cnt));
id2=cnt;
cnt++;
}
else
id2=(name.find(name2))->second;
Union(id1,id2);
int fid1=Find(id1);
printf("%d\n",Rank[fid1]);
}
}
}
}
int main()
{
IO();
return ;
}
hdu Virtual Friends的更多相关文章
- HDU 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 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 3172 Virtual Friends(map+并查集)
Virtual Friends Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tot ...
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
随机推荐
- 汇编学习(六)——代码转换程序
(一)逻辑运算指令 一.双操作数逻辑运算指令 1.指令格式: AND dst,src ; "与"运算, OR dst,src ; "或"运算 XOR dst,s ...
- pig的各种运行模式与运行方式详解
一.pig的运行模式: Pig 有两种运行模式: Local 模式和 MapReduce 模式.当 Pig 在 Local 模式运行的时候, Pig 将只访问本地一台主机:当 Pig 在 MapRed ...
- getTickCount()函数 VS GetTickCount()函数
这俩函数看上去长得太像了,笔者曾经马大哈地把两者当成一个函数了(确实长得很像),知道有一天发现返回的值离预期值差很远差仔细查了下. 1. getTickCount() 实际上,该函数为opencv中的 ...
- 关于初级java面试问题的一些整理 (部分转自他人)
1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部 ...
- 如何创建一个Android项目
第一步: File -->New ---->Android Application Project 点击创建 第二步:接下来是几个下拉选择框. Minimum Required SDK 是 ...
- hdu1087 最大递增子段和
http://acm.split.hdu.edu.cn/showproblem.php?pid=1087 状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0<=i<=j, ...
- css随记01编辑技巧,背景与边框
代码优化 一个按钮的例子,使其值同比例变化; button{ color: white; background: #58a linear-gradient(#77a0bb, #58a); paddin ...
- js小例子(简单模糊匹配输入信息)
该例子实现的是用户输入信息或者字母时可以搜索出来,鼠标点击选择 <!DOCTYPE html> <html> <style> p{ width:200px; hei ...
- iOS Xcode个人常用插件
1.AdjustFontSize 按command +/-进行字体大小调整 2.ATProperty @property专用,strong.assign.copy.weak IBOutlet 3.Ba ...
- JSP基础——创建、启动、浏览项目
创建的项目类型为WEB PROJECT. 新建的.JSP文件,可直接访问的放到WebRoot目录下,不可直接访问的就放到WEB-INF里. 要添加的.jar文件,复制到项目目录内,再右键Build P ...