带权并查集:HDU3172-Virtual Friends
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的更多相关文章
- HDU_3172_带权并查集
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
随机推荐
- 如何安装使用windows自带的telnet服务
控制面板->程序和功能->打开或关闭Windows功能->Telnet 客户端 [ Telnet 服务器 ] 安装完成后重启cmd telnet ip port
- AngularJS(七):表单-单选框、下拉列表
本文也同步发表在我的公众号“我的天空” 单选框 在AngulerJS中单选框的处理与复选框有不少相似之处,我们来看以下示例: <body ng-app="myApp" ng- ...
- ZR#331. 【18 提高 3】括号序列(栈)
题意 挺神仙的.首先$60$分暴力是比较好打的. 就是枚举左端点,看右端点能否是$0$ 但是这样肯定是过不了的,假如我们只枚举一次,把得到的栈记录下来 那么若区间$(l, r)$是可行的,那么$s_{ ...
- 《移动Web前端高效开发实战》笔记4--打造单页应用SPA
路由是一个单页应用的核心,大部分前端框架都实现了一个复杂的路由库,包括动态路由,路由钩子,组件生命周期甚至服务器端渲染等复杂的功能.但是对于前端开发者而言,路由组件的核心是URL路径到函数的映射,了解 ...
- linux系统基本结构-《循序渐进linux》
1.linux控制台 linux系统由桌面控制台(X -Window视窗)和字符控制台组成.字符控制台是linux的核心,默认linux下有6个字符控制台. 字符控制台--〉X-Window下:ctr ...
- Oracle listener.ora 设置
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 洛谷 P2383 狗哥玩木棒
题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第一行是一个整数n表示测试 ...
- Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)
dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...