这题是一个很简单额并查集的题目,首先第一步是要用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. Maven的简单使用,HelloWorld

    安装好Maven后就用一个简单的HelloWorld程序来测试一下,体验一下Maven.至于不懂的地方,请查看<Maven实战>书籍. 书籍网址:http://download.csdn. ...

  2. Tomcat 配置 Context

    在 conf/Catalina/localhost/ 下添加 xml配置文件,文件名和站点名一致. 配置文件示例 <?xml version='1.0' encoding='utf-8'?> ...

  3. set+几何 LA 5908 Tracking RFIDs

    题目传送门 题意:给一些传感器,范围在r内,再给一些询问点,问这些点能有几个传感器收到,当有墙隔绝时信号减弱,范围变小 分析:set存储传感器,用set的find来查找是否是传感器.因为询问点少,可以 ...

  4. MFC 打开链接的方法

    第一种: system("start explorer http://http://www.baidu.com"); 第二种: ShellExecute(NULL, NULL, _ ...

  5. Shell 编程基础之 While 练习

    一.语法 while [ condition ] # 当 condition 条件成立时,就进行循环,直到条件不成立停止 do #执行内容 done 二.练习 输入用户输入的参数,直到用户输入 &qu ...

  6. Python version 2.7 required, which was not found in the registry

    http://blog.csdn.net/zdnlp/article/details/12171687

  7. 【SQL】区分新来顾客和再访顾客

    -- 赋值 客户来访记录 SELECT m.* FROM (SELECT x.*, CASE WHEN x.ts > (SELECT MIN(a.ts) FROM USER.ps_afterre ...

  8. ACM 兄弟郊游问题

    兄弟郊游问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 兄弟俩骑车郊游,弟弟先出发,每分钟X米,M分钟后,哥哥带一条狗出发.以每分钟Y米的速度去追弟弟,而狗则以 ...

  9. ACM: I Hate It 解题报告 - 线段树

    I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  10. 洛谷 P1144 最短路计数 Label:水

    题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶点数与边数. 接下来M行 ...