这题是一个很简单额并查集的题目,首先第一步是要用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. I/O复用模型之epoll学习

    简介: epoll是linux下多路复用I/O接口select/poll的增强版,它能够显著提高程序在大量并发连接中只有少量活跃的情况下的系统cpu利用率,原因是它会复用文件描述符集合来传递结果而不用 ...

  2. Alcatraz安装 不能用解决方案

    1.安装 1>Github上下载Alcatraz,下载地址:https://github.com/supermarin/Alcatraz  2>Alcatraz是xcode的插件,这个插件 ...

  3. app分享时判断手机是否已安装微信或QQ客户端

    /** * 判断 用户是否安装微信客户端 */ public static boolean isWeixinAvilible(Context context) { final PackageManag ...

  4. JavaScript案例一:Window弹窗案例

    注:火狐可运行,谷歌不可运行(安全级别高) <!DOCTYPE html> <html> <head> <title>JavaScript 弹窗案例&l ...

  5. 【Tomcat】直接启动tomcat时为tomcat指定JDK 而不是读取环境变量中的配置

    在windows环境下以批处理文件方式启动tomcat,只要运行<CATALINA_HOME>/bin/startup.bat这个文件,就可以启动Tomcat.在启动时,startup.b ...

  6. python 继承

    继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...

  7. 关闭Android/iPhone浏览器自动识别数字为电话号码

    <meta name="format-detection" content="telephone=no"><meta http-equiv=& ...

  8. scau 8637 阶乘与因子 筛素数

    时间限制:500MS  内存限制:1000K提交次数:189 通过次数:46 题型: 编程题   语言: G++;GCC Description 游戏玩了很久总会厌的,连Lyd的蚂蚁都被放生了.... ...

  9. 简单的实现UIpicker上面的取消确定按钮

    1 因为我用的xib实现的添加picker 和textfiled的, @interface ViewController : UIViewController<UITextFieldDelega ...

  10. OpenCV(5) 对比度和亮度

      公式: 两个参数 \alpha > 0 和 \beta 一般称作 增益 和 偏置 参数.我们往往用这两个参数来分别控制 对比度 和 亮度 .   #include "stdafx. ...