题目链接

题目描述:

输入
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. 2018年江西理工大学C语言程序设计竞赛高级组部分题解

    B Interesting paths 考察范围:组合数学 此题是机器人走方格的变种,n*m的网格,从(1,1)走到(n,m),首先可以明确,水平要走m-1格,竖直要走n-1格,则走到目的地的任意一条 ...

  2. 响应式网页设计:互联网web产品RWD概念

    RWD(Responsive Web Design)可称为自适应网页设计.响应式网页设计.响应式网页设计等等,是一种可以让网页的内容可以随着不同的装置的宽度来调整画面呈现的技术,让使用者可以不需要透过 ...

  3. this self指针

    this 和 self指针 为函数提供了运行上下问:为函数提供了当前对象的其实地址,方便函数的对对象的访问.

  4. JS判断客户端是否是iOS或者Android或者ipad(三)

     *  * @function: 判断浏览器类型是否是Safari.Firefox.ie.chrome浏览器  * @return: true或false  *  */ function isSafa ...

  5. Java之秒杀活动解决方案

    0 引言 本文主要描述,服务端做相关秒杀活动的时候,对应的解决方案,即高并发下的数据安全. 1 优化方案 1.1 乐观锁思路 Redis中的watch,请求时,通过Redis查询当前抢购数据,如果当前 ...

  6. IOS - 网络指示器

    #pragma mark Activity methods - (void)openActivity { // 添加网络指示器 activityIV = [[UIActivityIndicatorVi ...

  7. [Vue warn]: Invalid prop: custom validator check failed for prop "type".

    遇到错误如下, [Vue warn]: Invalid prop: custom validator check failed for prop "type". found in ...

  8. Qt Designer设计 UI 文件并调用

    本文介绍的是Qt Designer设计 UI 文件并调用,在坛子里逛了一圈,关于UI方面的好像不怎多,本篇给大家分享一下. AD: 2013云计算架构师峰会超低价抢票中 Qt Designer设计 U ...

  9. 20121124.Nodejs创建HTTP程序.md

    ####1.源代码: var http=require('http');//读取http模块    http.createServer(function(req,res){//创建一个服务,接受一个回 ...

  10. js实现点击复制网页内容(基于clipboard.js)

    浏览网页过程中会遇到点击复制链接地址的情况,下面就介绍一种实现方法,该方法是基于clipboard.js: 官网地址:https://clipboardjs.com/: clipboard.js使用比 ...