并查集解1335 : Email Merge(hihocoder)
来源:互联网 发布:linux 查看函数头文件 编辑:程序博客网 时间:2024/05/18 14:12
题目
描述
You are given a list of usernames and their email addresses in the following format:
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
Your task is to merge the usernames if they share common email address:
alice alicebest alice2016
bob
输入
The first line contains an integer N, denoting the number of usernames. (1 < N <= 10000)
The following N lines contain N usernames and their emails in the previous mentioned format.
Each username may have 10 emails at most.
输出
Output one merged group per line.
In each group output the usernames in the same order as the input.
Output the groups in the same order as their first usernames appear in the input.
样例输入
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
题目解析
题目给定n个人,每个人使用不多于10个的邮箱,要求找出共享邮箱的人(比如a使用123@qq.com, b使用345@qq.com和123@qq.com, c使用345@qq.com,则a,b,c共享邮箱),要求找出这些类的人并按照输入顺序输出
思路解析
这就是并查集的简单使用样例,直接套入并查集的代码即可,注意要使用STL容器存储相关信息,以增快速率,比如用map存储邮箱以及该邮箱的使用者,这样能增加遍历速度,不然就可能出现TLE的情况
AC代码
//// Created by YoshiPark on 2017/10/16.//#include <iostream>#include <vector>#include <map>using namespace std;int F[10010];void init(int num) { for (int i = 0; i < num; ++i) { F[i] = i; }}int getfather(int v) { if (v == F[v]) { return v; } else{ F[v] = getfather(F[v]); return F[v]; }}int merge(int a, int b) { int fa = getfather(a); int fb = getfather(b); if (fa != fb) { if (fa > fb) { F[fa] = fb; } else{ F[fb] = fa; } }}int main() { int num; cin >> num; init(num); map<string, vector<int>> maillist; vector<string> userlist; for (int i = 0; i < num; ++i) { string username; int mailnum; cin >> username >> mailnum; userlist.push_back(username); for (int j = 0; j < mailnum; ++j) { string mail; cin >> mail; maillist[mail].push_back(i); } } for (auto it = maillist.begin(); it != maillist.end(); ++it) { for (int j = 1; j < it->second.size(); ++j) { merge(it->second[0], it->second[j]); } } vector<int> group[10010]; for(int i = 0; i< num; i++){ if(getfather(i) != i) group[getfather(i)].push_back(i); } for(int i = 0; i< num; i++){ if(getfather(i) == i){ cout<< userlist[i]; for(int j = 0; j<group[i].size(); j++){ cout<<" "<< userlist[group[i][j]]; } cout<<endl; } }}
- 并查集解1335 : Email Merge(hihocoder)
- [Hihocoder]1335 : Email Merge (并查集)
- hiho 1335 Email Merge
- Email Merge——hihoCoder171
- Email Merge hiho一下第171周
- hiho一下第171周《Email Merge》
- hihocoder:无间道之并查集
- hihoCoder-随机产生数字并排序
- 【并查集】 hihocoder #1160 : 攻城略地
- hihocoder#1066之并查集
- hihocoder 1066 并查集java实现
- hihocoder 无间道之并查集
- hihocoder #1515 : 分数调查 并查集好题
- hihocoder week171分析---并查集
- hihoCoder 无间道之并查集
- 关于email合法性判断,并将非法的email改正为正确的email(正则表达式)
- JavaMelody 简单配置 并发送 Email
- Git查看并修改name和email
- 二叉树深度优先搜索(DFS)、广度优先搜索(BFS)
- 反转加密与凯撒加密 python实现
- fdsfds
- Contest 3:Problem A: Number and its reverse
- 文件上传----不使用submit提交
- 并查集解1335 : Email Merge(hihocoder)
- C语言学习笔记(五)——复合类型
- 算法之选择排序
- ECLIPSE生成JAVADOC时出错:编码GBK的不可映射字符
- 回车事件
- HDU4670-Cube number on a tree
- hdu 2639 (Bone Collector II) 0-1背包的第k优解
- NOIP 2002 均分纸牌
- Filter过滤器