Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8229    Accepted Submission(s): 2363

Problem Description
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.

 
Input
Input 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).
 
Output
Whenever 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
 
带权并查集:也就是可以记录每个集合中元素的个数。
实现方法:在普通并查集中增加dist数组在记录每个父节点的深度(即该集合中元素个数)
#include<iostream>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
#define N 200005 map<string,int>m;
int father[N],dist[N]; int Find(int x)
{
if(father[x]!=x)
father[x]=Find(father[x]);
return father[x];
} void Merge(int a,int b)
{
int fa=Find(a);
int fb=Find(b);
if(fa!=fb)
{
father[fa]=fb;
dist[fb]+=dist[fa];
}
printf("%d\n",dist[fb]);
} int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
m.clear();
int n,cnt=;
string name1,name2;
scanf("%d",&n);
for(int i=; i<=*n; i++)
{
father[i]=i;
dist[i]=;
}
while(n--)
{
cin>>name1>>name2;
if(m.find(name1)==m.end())
m[name1]=cnt++;
if(m.find(name2)==m.end())
m[name2]=cnt++;
Merge(m[name1],m[name2]);
}
}
}
return ;
}

HDU_3172_带权并查集的更多相关文章

  1. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  2. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  3. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  4. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  5. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  6. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  7. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  8. poj1984 带权并查集

    题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...

  9. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

随机推荐

  1. C#.NET如何判断是否有缺少的using

    调试的时候会报错,红色的波浪线表示出错的位置,右击即可找到对应的using      

  2. Java 实现一个链表

    public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...

  3. Highcharts构建加权平均值图表

    Highcharts构建加权平均值图表 加权平均值图表是将图表中多个数据列值.依据加权算法计算获取平均值,并加入生成一个加权折线.在这里,我们直接使用第三方插件Dynamic Weighted Ave ...

  4. 卷积神经网络-进化史】从LeNet到AlexNet

    目录视图 摘要视图 订阅 [置顶] [卷积神经网络-进化史]从LeNet到AlexNet 标签: cnn 卷积神经网络 深度学习 2016年05月17日 23:20:3046038人阅读 评论(4)  ...

  5. Creo二次开发—内存处理

    #include <ProDisplist.h> ProError ProDisplistInvalidate(ProMdl model) Invalidates the two- or ...

  6. Selenium Webdriver弹出框 微博分享的内容控制与结果生成

    browser.window_handles for i in ugc_url_l: js = 'window.location.href="{}"'.format(i) brow ...

  7. Git经常使用命令总结

    Git是一款开源的分布式版本号控制系统,由Linux之父Torvalds用C语言开发. "the stupid content tracker",Git自诩为stupid,却是一个 ...

  8. 【HDU 4547】 CD操作

    [题目链接] 点击打开链接 [算法] 分四种情况讨论 : 1. 当前目录和目标目录是同一目录,不需要变换,答案为0 2. 当前目录是目标目录的祖先,答案为当前目录的深度 - 目标目录的深度 3. 当前 ...

  9. 洛谷P2680 运输计划——树上差分

    题目:https://www.luogu.org/problemnew/show/P2680 久违地1A了好高兴啊! 首先,要最大值最小,很容易想到二分: 判断当前的 mid 是否可行,需要看看有没有 ...

  10. JeePlus:项目部署

    ylbtech-JeePlus:项目部署 1.返回顶部 1. 项目部署 1 开发工具:idea/eclipse/myeclipse+ mysql/oracle+tomcat6/7/8. 下面以ecli ...