【codeforces】Misha and Changing Handles(映射+并查集)

向右看齐 2022-09-26 11:59 107阅读 0赞

Misha and Changing Handles

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit Status

Description

Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.

Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.

Input

The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.

Next q lines contain the descriptions of the requests, one per line.

Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.

The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handlenew is not used and has not been used by anyone.

Output

In the first line output the integer n — the number of users that changed their handles at least once.

In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old andnew, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.

Each user who changes the handle must occur exactly once in this description.

Sample Input

Input

  1. 5
  2. Misha ILoveCodeforces
  3. Vasya Petrov
  4. Petrov VasyaPetrov123
  5. ILoveCodeforces MikeMirzayanov
  6. Petya Ivanov

Output

  1. 3
  2. Petya Ivanov
  3. Misha MikeMirzayanov
  4. Vasya VasyaPetrov123

题意:让求两个最终关联的id名称并输出,中间id略过输出。

写法:先map映射一下,并用一个字符数组记录当前数字对应的整数值;然后对数字用并查集进行关联;把结果保存在另一个字符数组中输出即可。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<map>
  5. #include<cmath>
  6. #include<algorithm>
  7. using namespace std;
  8. const int N = 2020;
  9. char c[N][22],d[N][22];
  10. bool vis[N];
  11. int pre[N];
  12. int find(int x) {
  13. int r=x;
  14. while(r!=pre[r]) {
  15. vis[pre[r]]=1;
  16. r=pre[r];
  17. }
  18. return r;
  19. }
  20. //void Union(int x,int y) {
  21. // int fx=find(x);
  22. // int fy=find(y);
  23. // if(fx!=fy) {
  24. // pre[fx]=fy;
  25. // }
  26. //}
  27. int main() {
  28. int n;
  29. while(scanf("%d",&n)!=EOF) {
  30. memset(vis,0,sizeof(vis));
  31. map<string,int>mp;
  32. mp.clear();
  33. char a[22],b[22];
  34. int pos=0;
  35. for(int i=0; i<N; i++) {
  36. pre[i]=i;
  37. }
  38. for(int i=0; i<n; i++) {
  39. scanf("%s%s",a,b);
  40. if(!mp[a]) mp[a]=++pos,strcpy(c[pos],a);
  41. if(!mp[b]) mp[b]=++pos,strcpy(c[pos],b);
  42. pre[mp[a]]=mp[b];
  43. }
  44. int temp=0,num=0;
  45. for(int i=1; i<=pos; i++) {
  46. if(!vis[i]) {
  47. vis[i]=1;
  48. int cnt=find(i);
  49. strcpy(d[num++],c[i]);
  50. strcpy(d[num++],c[cnt]);
  51. temp++;
  52. }
  53. }
  54. printf("%d\n",temp);
  55. for(int i=0; i<num; i++) {
  56. printf("%s%c",d[i],i&1?'\n':' ');
  57. }
  58. }
  59. return 0;
  60. }

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读

    相关

    并查集JAVA版框架 并查集是一种用来管理元素分组情况的数据结构。 并查集可以高效地进行如下操作: \--查询元素a和元素b是否属于同一组

    相关

    这个文章是几年前水acm的时候转的, 当时也不知道作者是谁, 要是有人知道的话说一下吧 并查集是我暑假从高手那里学到的一招,觉得真是太精妙的设计了。以前我无法解决的

    相关

    > 题目 > 某学校近期要组织全校同学出去参加某项活动,由于人数众多,学校决定让同学们自行组队,以小组为单位进行活动。假设学校一共n个同学,每个同学有一个唯一的数字作为标签

    相关

    森林: 森林是由若干棵互不相交的树组成,两棵树分别独立,没有交集 ![20181112082744488.png][] 并查集: 并查集的结构和森林十分相似,是

    相关

    来看一个实例,[杭电1232畅通工程][1232] 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的。最后要解决的是整幅图的连通性

    相关

    并查集的作用就是快速判断两个元素是否在同一个集合中,快速将两个集合合并 基本模板 include <iostream> include <a