题目链接

题目描述:

输入
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. CADisplayLink & NSTimer

    屏幕刷新与UI更新不同步:屏幕刷新由硬件(+GPU)保证,UI更新由软件(CPU保证). 出现卡顿的原因是软件的计算速度跟不上硬件的刷新速度. 一 简介 1 所在框架 CADisplayLink和其它 ...

  2. 脚本_使用expect自动交互远程主机安装软件

    #!bin/bash#功能:使用expect工具自动交互密码,远程到其它主机,安装httpd软件#作者:liusingbon#删除~/.ssh/known-hosts后,ssh远程任何主机,系统都会询 ...

  3. CF993C Careful Maneuvering bitset_枚举

    Code: #include<cstdio> #include<map> #include<iostream> #include<cmath> #inc ...

  4. 系统中 CPU 时间片是多久

    Windows 系统中线程轮转时间也就是时间片大约是20ms,如果某个线程所需要的时间小于20ms,那么不到20ms就会切换到其他线程:如果一个线程所需的时间超过20ms,系统也最多只给20ms,除非 ...

  5. [NOIP补坑计划]NOIP2017 题解&做题心得

    终于做完了…… 场上预计得分:?(省一分数线:295) 由于看过部分题解所以没有预计得分qwq 题解: D1T1 小凯的疑惑 题面 震惊!一道小学奥数题竟难倒无数高中考生! 欢迎大家以各种姿势*和谐* ...

  6. H5-移动端适配

    之前写H5页面也会遇到适配问题, 是通过媒体查询一点一点调整,始终觉得很繁琐,但一直也没去想想解决的办法. 今天专门花了一上午的时间来去研究.  小生只是刚踏入前端路的小白,对于网上各位大佬的讲解适配 ...

  7. CF449D Jzzhu and Numbers (状压DP+容斥)

    题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...

  8. 小白都能看懂的Linux系统下安装配置Zabbix

    实验环境: 操作系统:Centos 7.6 服务器ip:192.168.10.100 运行用户:root 网络环境:Internet Zabbix是一个基于web界面的提供分布式系统监控及网络功能的企 ...

  9. web前端项目规范

    项目目录规范 . ├─ css ├─ component ├─ img ├─ js ├─ page ├─ test ├─ package.json ├─ README.md css 存放样式类文件,且 ...

  10. tomcat闪退无法启动 the catalina_home environment variable is not defined correctly this environment variable is needed to run this program

    未成功配置CATALINA_HOME 1.计算机>属性>环境变量, 新建环境变量.变量名为CATALINA_HOME ,变量值tomcat的解压目录,注意后面不用多加“\”或者“;” 2. ...