这题是一个很简单额并查集的题目,首先第一步是要用map将字符串映射为整型,这样方便后面的处理,然后就是用一个rank[]数组来记录每个朋友圈的人数。之后就是简单的并查集操作了。

这里给出一组测试案例:

1
6
Fred Barney
Barney Betty
Betty Wilma
AAAAA BBBBB
AAAA  BBBBB
AAAA Fred

输出结果应是:
2
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的更多相关文章

  1. HDU Virtual Friends(超级经典的带权并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  3. hdu 3172 Virtual Friends (映射并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. hdu 3172 Virtual Friends

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...

  5. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  6. HDU 3172 Virtual Friends(map+并查集)

    Virtual Friends Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tot ...

  7. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. Virtual Friends HDU - 3172 (并查集+秩+map)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  9. 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. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  2. Android自动化测试之Monkey Test(一)

    Monkey是什么 Monkey是可以运行在模拟器里或实际设备中的程序.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.   Monkey简 ...

  3. java 汽车销售收入系统

     >>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...

  4. OpenCV 线性混合(4)

      带滚动条的线性混合示例:   #include "stdafx.h" #include<iostream> #include<thread> #incl ...

  5. storm进程正常运行一段时间shut down,运维方式

    storm启动一段时间后,无征兆的停止了,然后nimbus,supervisor,ui所有的worker都stop了. 我用的storm是0.8.2版本的 nimbus中留下的log如下 -- :: ...

  6. linux 查看机器的cpu,操作系统等命令

    看cpu信息,型号,几核 [root@f3 ~]# cat /proc/cpuinfo | grep name | cut -f2 -d:| uniq -c 16 Intel(R) Xeon(R) C ...

  7. HDU1853 Cyclic Tour(最小费用最大流)

    题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...

  8. Hangover[POJ1003]

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121079   Accepted: 59223 Descr ...

  9. unity

    static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object ...

  10. 【BZOJ1864】[Zjoi2006]三色二叉树 树形DP

    1864: [Zjoi2006]三色二叉树 Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最 ...