题目链接

题目描述:

输入
4
alice 2 alice@hihocoder.com alice@gmail.com
bob 1 bob@qq.com
alicebest 2 alice@gmail.com alice@qq.com
alice2016 1 alice@qq.com
输出
alice alicebest alice2016
bob

如上所示,每一行前面是用户名,后面是他的邮箱,如果两个人共用了一个邮箱说明他是同一组的。

输出分组后的结果。一组占一行。组间顺序和组内顺序保证和输入相同。

数据大小是:最多10000个人,每个人最多10个email

The first line contains an integer N, denoting the number of usernames. (1 < N ≤ 10000)

Each username may have 10 emails at most.

-----------------------------------------------------------------------------------------------------------------------------------------

看着简单,还有有些细节需要注意的,比如顺序的保证。

上来就想建图求联通分量,想了一下没必要:如果每组都有10个email,则需要建边10*9*1w=90w条边,太麻烦了

后来一想用并查集做既省空间又省时间,每组的email都merge到每组的第一个上。这样每组的第一个email的父亲就对应着一个分组。

#include <map>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = * + ; int father[N];
int find(int id){
int fid = father[id];
if(fid==id) return fid;
return father[id]=find(fid);
}
void merge(int a,int b){
int fa = find(a);
int fb = find(b);
if(fa==fb) return;
father[fb] = fa;
} struct USER_MAILID{
char name[];
int mailId;
};
USER_MAILID names[]; vector<string > output_list[];
map<int,int> father_ouputId; int main(){
for(int i=;i<N;i++) father[i]=i; map<string,int> mail_id_mapper; int mailId = ; int n,cnt; cin>>n;
char strbuf[]; int id_buf[];
for(int id=;id<n;id++){
scanf("%s%d",names[id].name,&cnt);
for(int i=;i<cnt;i++){
scanf("%s",strbuf);
auto iter = mail_id_mapper.find(strbuf);
if(iter==mail_id_mapper.end()){
mail_id_mapper[strbuf] = (id_buf[i] = mailId++);
}
else{
id_buf[i] = iter->second;
}
}
names[id].mailId = id_buf[]; for(int i=;i<cnt;i++) for(int j=i+;j<cnt;j++){
merge(id_buf[i],id_buf[j]);
}
}
int curId = ; int outputId = ;
for(int id=;id<n;id++){
int f = find(names[id].mailId);
auto iter = father_ouputId.find(f);
if(iter==father_ouputId.end()){
father_ouputId[f] = curId = outputId++;
}
else curId = iter->second;
output_list[curId].push_back(names[id].name);
}
for(int i=;i<outputId;i++){
for(int j=;j<output_list[i].size();j++){
printf("%s ",output_list[i][j].c_str());
}puts("");
}
return ;
}

hiho 171周 - 水题,并查集的更多相关文章

  1. Uva 10596 - Morning Walk 欧拉回路基础水题 并查集实现

    题目给出图,要求判断不能一遍走完所有边,也就是无向图,题目分类是分欧拉回路,但其实只要判断度数就行了. 一开始以为只要判断度数就可以了,交了一发WA了.听别人说要先判断是否是联通图,于是用并查集并一起 ...

  2. Jzoj 初中2249 蒸发学水(并查集)

    题目描述 众所周知,TerryHu 是一位大佬,他平时最喜欢做的事就是蒸发学水. 机房的位置一共有n 行m 列,一开始每个位置都有一滴学水,TerryHu 决定在每一个时刻选择 一滴学水进行蒸发,直到 ...

  3. NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题

    dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. C#LeetCode刷题-并查集

    并查集篇 # 题名 刷题 通过率 难度 128 最长连续序列   39.3% 困难 130 被围绕的区域   30.5% 中等 200 岛屿的个数   38.4% 中等 547 朋友圈   45.1% ...

  6. luogu5012 水の数列 (并查集+线段树)

    如果我们能求出来每个区间个数的最大分值,那就可以用线段树维护这个东西 然后出答案了 然后这个的求法和(luogu4269)Snow Boots G非常类似,就是我们把数大小排个序,每次都拿<=x ...

  7. 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel

    有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...

  8. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  9. hiho #1066 : 无间道之并查集

    #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之,小Hi和小H ...

随机推荐

  1. SQL Server 从字符串中提取中文、英文、数字

    --[提取中文] IF OBJECT_ID('dbo.fun_getCN') IS NOT NULL DROP FUNCTION dbo.fun_getCN GO create function db ...

  2. Combox程序中自定义数据源

    有combox控件,命名为cbxPutStatus,在程序中为其手工定义数据源,并绑定 private void POrderSplitFrm_Load(object sender, EventArg ...

  3. SQL学习——基础语句(3)

    先上数据表 student表 grade 表 SQL Union 语句 合并两个或多个select查询结果集. select s_id from student union select  s_id ...

  4. MDL的一些理解

    驱动程序要操作一个用户模式下的内存(32位下小于2G),那么是有风险的,因为用户模式下当前进程的线程不断切换,用户模式下的地址可能会无效.这时的操作将会有未知结果. 用MDL系统API可以将用户模式下 ...

  5. tcpsock.v2 与 ecocache

    因为很不满意 tcpsock 的设计与实现,及有意专为譬如游戏服务器端开发设计一套 TCP 网络库,所以年初即有了 tcpsock.v2 的开发计划,于是粗略整理出了以下几条目标计划: 1) TcpC ...

  6. 【BZOJ1396】识别子串 - 后缀自动机+线段树

    题意: Description Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 题解: ...

  7. jQuery中的DatePicker今天按钮不起作用

    转载:http://codego.net/63433/ jquery-ui 日期选择器datepicker我想用 jQueryUI 的 DatePicker ,并显示“今天”按钮, 但它不工作,它也不 ...

  8. 2019-03-18 用Task Schedule定时调用Python脚本

    1.空白处右键新建Task(Create New Task)设置每日定时(Daily) 2.新建Actions 3.填写 Program+Start in 为Python的路径:C:\python36 ...

  9. 在Vue中使用了Swiper ,动态从后台获取数据的之后,swiper滑动失效??

    在Vue中使用了Swiper ,动态从后台获取数据的之后,swiper滑动失效?? 是因为swiper提前初始化了,那时候数据还没有完全出来.这里有两种解决办法 1. 使用vue提供的$nextTic ...

  10. ZOJ 3288 Domination

    D - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Descr ...