These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

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.

InputInput file contains multiple test cases. 
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).OutputWhenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.Sample Input

1
3
Fred Barney
Barney Betty
Betty Wilma

Sample Output

2
3
4 题意:找朋友,输出相应的朋友数量
思路:就是并查集加个秩,在用map存一下,方便输出
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
const int maxn=;
map<string,int>p;
int pre[maxn],ran[maxn],maxx;
void init()
{
int i;
for(i=;i<maxn;++i)
{
pre[i] = i;
ran[i]=;
}
} int find(int x)
{
if(x!=pre[x])
{
pre[x] = find(pre[x]);
}
return pre[x];
} void combine(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx!=fy)
{
pre[fx] = fy;
ran[fy] += ran[fx];
}
} int main()
{ int n,m;
char name[],na[];
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
p.clear();
init();
int k=;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%s %s",name,na);
if(p.find(name)==p.end())
p[name]=k++;
if(p.find(na)==p.end())
p[na]=k++;
combine(p[name],p[na]);
printf("%d\n",ran[find(p[na])]);
}
}
}
return ;
}

Virtual Friends HDU - 3172 (并查集+秩+map)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

  2. WPF机制和原理

    最近由于项目需要,自己学习了一下WPF,之前接触过sliverlight,所以对理解和编写XAML不是太陌生.其实XAML和html多少还是有点类似的.只不过XAML上添加上了自动binding机制( ...

  3. h5点击区域和实际区域对不上

    点击区域和实际区域对不上 然后点击后触发的其实是上面的区域,会导致事件触发错误

  4. IO(转换流、缓冲流)

    第1章 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者Output ...

  5. JSTORM中IRichBolt与IBasicBolt的区别

  6. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here

    你的飞碟在这儿 难度:☆ Code: #include<iostream> #include<cstring> #include<cstdio> using nam ...

  7. VC++堆栈大小设置

    VC++堆栈默认大小是1M,如果你分配大于1M的堆,一般会出异常,这里你要把堆调大些,下面是VC++6.0与VC++2010的设置方法 VC++6.0: 工程==>设置==>“连接”界面, ...

  8. Bot Framework:Activity类简明指南

    Bot Framework相关文档:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html B ...

  9. java 访问docker的环境

    1.   配置环境 新增 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock root@ros ...

  10. Unity3D中使用Projector生成阴影

    在Unity3D中使用Projector实现动态阴影 无意中看见一篇博客叙述使用Projector实现动态阴影可以在移动平台拥有非常好的性能,遂按照其想法实现了一遍,发现其中竟有许多细节,写下这篇博客 ...