Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9179 Accepted Submission(s): 2654

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


解题心得

  • 这个是一个带权并查集,其实也没有什么新奇的,主要就是需要记录每一个string的根,这个可以用一个map来映射一下,将每一个string映射成一个数字,然后用映射的数字来进行合并就行了。
  • 这个题需要输出的东西是当前建立关系的网络里面的人数,所以需要用数组来记录一下当前根的人数就可以了。
  • 其实这个题主要处理的就是一个关于string的问题,当需要将一个string用一个数子来代替的时候可以很流畅的想到map 的映射,具体的映射操作看代码。

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+10;
int father[maxn*2],ans,num[maxn*2];
map <string,int> mp;//string映射成一个number int Find(int x)
{
if(father[x] == x)
return x;
else
return father[x] = Find(father[x]);
} void _merge(int a,int b)
{
int fa = Find(a);
int fb = Find(b);
if(fa != fb)
{
father[fa] = fb;
num[fb] += num[fa];//合并的时候要将不同根的网络的人数也合并起来
}
printf("%d\n",num[fb]);
} int main()
{
int t;
while(scanf("%d",&t) != EOF)
{
while(t--)
{
int n;
mp.clear();
ans = 1;
scanf("%d",&n);
int cnt = 0;
for(int i=0; i<=n; i++)
{
father[i] = i;
num[i] = 1;
}
for(int i=0; i<n; i++)
{
string s1,s2;
cin>>s1>>s2;
//将string用一个数字来表示
if(mp.find(s1) == mp.end())
mp[s1] = cnt++;
if(mp.find(s2) == mp.end())
mp[s2] = cnt++; _merge(mp[s1],mp[s2]);
}
}
}
}

带权并查集:HDU3172-Virtual Friends的更多相关文章

  1. HDU_3172_带权并查集

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

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

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

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

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

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

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

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

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

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

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

  7. hdu3038(带权并查集)

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

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

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

  9. poj1984 带权并查集

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

随机推荐

  1. 前端html与css学习笔记总结篇(超详细)

    第一部分 HTML 第一章 职业规划和前景 职业方向规划定位: web前端开发工程师 web网站架构师 自己创业 转岗管理或其他 web前端开发的前景展望: 未来IT行业企业需求最多的人才 结合最新的 ...

  2. 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式

    1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...

  3. Android Studio maven-metadata.xml 卡着不动原因和解决方法

    头一天好好的,第二天就卡着了. 一直在这个地方不动,如果停止就会报 Error:Could not run build action using Gradle distribution ‘https: ...

  4. ios uilabel 根据文字 计算宽度 高度

    //根据宽度求高度        + (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFl ...

  5. 读写属性/if判断那点事/数组

    读写属性属性操作注意事项 js中不允许出现"-".所以font-size变成fontSize/margin-top变成marginTop. Op.style.with=" ...

  6. ./theHarvester.py -d baidu.com -l 100 -b google

    ./theHarvester.py -d baidu.com  -l 100 -b google

  7. 2017微软骇客马拉松精彩大回Fun:不一样的Hacker,一Young的Cool

    丹棱君有话说:一年一度激动人心的骇客马拉松大会结束了!这场内部创意大比拼硕果累累,丹棱君准备好了 6 组 Cool 骇客的别 Young 作品——沉浸式销售工具如何能守得“云”开见月明?“骇客马拉松超 ...

  8. Exchange 用户邮箱导入/导出

    在第2部分中,我将向您介绍如何使用Exchange Server中提供的新cmdlet导入/导出数据,以及如何查看导入和导出的信息统计信息这样做. 走起! 将数据从PST文件导入到邮箱 现在是时候尝试 ...

  9. [神经网络]一步一步使用Mobile-Net完成视觉识别(五)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第五篇,讲解如何调用测试训练结果. 上一篇中我们输出了训练的模型,这一篇中我们通过调用训练好的模型来完 ...

  10. DROP GROUP - 删除一个用户组

    SYNOPSIS DROP GROUP name DESCRIPTION 描述 DROP GROUP 从数据库中删除指定的组.组中的用户不被删除. 组中的用户不被删除. PARAMETERS 参数 n ...