HDU 3172 Virtual Friends (map+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172
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 题解:带权并查集 可以记录每个集合中元素的个数
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=;
const int mod=1e9+;
map<string,int> mp;
int sum[N],f[N];
void init()
{
for(int i=;i<N;i++)
f[i]=i,sum[i]=;
}
int Find(int x)
{
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int Union(int a,int b)
{
int p=Find(a);
int q=Find(b);
if(p!=q){
f[p]=q;
sum[q]+=sum[p];
}
return sum[q];
}
int main()
{
int t,n;
while(scanf("%d",&t)!=EOF){
while(t--){
mp.clear();
scanf("%d",&n);
init();
int num=;
char a[],b[];
while(n--){
int u,v;
scanf("%s%s",a,b);
if(mp[a]==) mp[a]=num++;
if(mp[b]==) mp[b]=num++;
u=mp[a],v=mp[b];
int k=Union(u,v);
printf("%d\n",k);
}
}
}
return ;
}
HDU 3172 Virtual Friends (map+并查集)的更多相关文章
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- hdu 3172 Virtual Friends (映射并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 2419 Boring Game(并查集+map)
感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...
- hdu 3172 Virtual Friends
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
随机推荐
- Amazon SQS(Simple Queue Service) 简单介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hongchangfirst/article/details/25877059 SQS即Simple ...
- python变量交换及注释种类,注释注意事项/注释排查方法
#小练习作业 # texe1 = '能提笔安天下' # print(texe1) # string1 = '武能上' # string2 = '定乾坤' # print(string1,string2 ...
- Hive和SparkSQL:基于 Hadoop 的数据仓库工具
Hive 前言 Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的 SQL 查询功能,将类 SQL 语句转换为 MapReduce 任务执行. ...
- 如何写一个优秀的GitHub项目README文档?
今天给大家介绍一个Github上的README文档写作教程模版,该模版目前获得6634颗星星,2296Fork,相对而言,还是比较得到大家认可的.不花哨,不别出心裁,一个比较实用的,普适性的架子:所谓 ...
- [附POC]Apache Struts2最新(CVE-2017-5638,S02-45)POC
#! /usr/bin/env python # encoding:utf-8 import urllib2 import sys from poster.encode import multipar ...
- Error #2148
参考:https://blog.csdn.net/watersevenmmfx/article/details/52980804 chrome flash 安全沙箱冲突 SecurityError: ...
- teragen/terasort_简化版
1, 关闭Hadoop安全模式 进入hdfs用户 su – hdfs Cd /opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/bin hdfs dfs ...
- HDU 2842 Chinese Rings(常数矩阵)
Chinese Rings 转载自:点这里 [题目链接]Chinese Rings [题目类型]常数矩阵 &题意: 一种中国环,解开第k个环需要先解开全部的前(k-2)个环,并留有第(k-1) ...
- 010-判断是否回传IsPostBack属性
属性IsPostBack:判断是否回传 如果是第一次请求,则返回false 如果是回传请求,则返回true->说明:只有使用服务器端表单才可以正常使用IsPostBack->使用情境:当在 ...
- jQuery-手风琴伸缩效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...