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 andnew 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 handle new 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 and new, 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.

Example

Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123
 /*
Name: Misha and Changing Handles
Copyright:
Author:
Date: 10/08/17 09:34
Description:给定多个改名的查询,每个查询包括一个新名字和旧名字,一个人可以多次更改,
最终得到一个新名字,求这些查询中一共有多少个人,
并且输出他最初的名字和最后的名字。(1<=q<=100)
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std; int main()
{
int t;
map<string,string>Users;
string old,newl;
while(cin>>t)
{
while(t--)
{
cin>>old>>newl;
if(!Users.count(old))
{
Users[old] = old;
}
Users[newl] = Users[old];
Users.erase(old);
}
cout<<Users.size()<<endl;
map<string,string>:: iterator item;
for(item = Users.begin();item != Users.end();item++)
cout<<(*item).second<<" "<<(*item).first<<endl;
} return ;
}
/*
把(A,B),(B,C)-->(A,C)
使用map,需要在输入后处理和输出处处理
(A,B),(B,C)-->(B,A),(C,B)-->(C,A)-->(A,C)
*/

ACM Misha and Changing Handles的更多相关文章

  1. 【CodeForces - 501B 】Misha and Changing Handles(map)

    Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description  神秘的三角洲里还有一个传说 ...

  2. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  3. B. Misha and Changing Handles

    B. Misha and Changing Handles time limit per test 1 second memory limit per test 256 megabytes input ...

  4. Misha and Changing Handles

    Description Misha hacked the Codeforces site. Then he decided to let all the users change their hand ...

  5. CodeForces 501B Misha and Changing Handles(STL map)

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  6. CodeForces 501B - Misha and Changing Handles

    有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...

  7. codeforces 501 B Misha and Changing Handles 【map】

    题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解 ...

  8. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. IT技术有感

    今天看技术文章,spring相关的,某一个点以前每次看一直不理解, 可是不知道为什么隔了1年左右,中间什么都没做,现在却都懂了. 在看懂的那一刻,笼罩在我心上的躁动突然平静了许多,我的心这一年来前所未 ...

  2. python基础——内置函数

    python基础--内置函数  一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...

  3. UI前端开发都是做什么的以及html、css、php、js等究竟是神马关系

    第一个问题: 1.UI,是视觉方面的呈现.一个网页首先由UI完成整体设计,然后把每一个模块切图,例如组件.logo.版块等.常用工具:PS,AI,DW. 2.前端,是将UI的设计代码化,因为计算机无法 ...

  4. 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

    定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) import urllib.request def get_ ...

  5. jQuery 事件绑定 和 JavaScript 原生事件绑定

    总结一下:jQuery 事件绑定 和 JavaScript 原生事件绑定 及 区别 jQuery 事件绑定 jQuery 中提供了四种事件监听绑定方式,分别是 bind.live.delegate.o ...

  6. 0417 jsBom操作+Dom再次整理

    BOM 1.Windows对象 window.open("打开的地址","打开的位置")window.opener:打开此页面的上一个页面对象window.cl ...

  7. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. [LeetCode] Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  9. Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】

    原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...

  10. xcode8的那些坑儿

    前几天手又贱,更新了xcode8....被几个坑玩坏了.最起码,字体改了,我现在还没有适应.下面列举了这两天遇到的问题 1.关于相册,照相,通讯录,麦克风的权限问题 xcode8打完包安装后,你会发现 ...