302593: CF501B. Misha and Changing Handles

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Misha and Changing Handles

题意翻译

白面鸮在维护罗德岛的干员数据库。其中有些干员多次改变姓名,白面鸮想知道干员最初的姓名和当前的姓名(干员可多次改名)。请您帮助她。 input 一行n,表示有n次修改操作(1<=n<=1000) 接下来n行,每行2个字符串,长度不过20,以空格相隔。 output 一行,输出有m个干员(输出顺序任意) 接下来m行,每行2个字符串,表示干员最初姓名和当前姓名。

题目描述

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.

输入输出格式

输入格式


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 handle $ new $ is not used and has not been used by anyone.

输出格式


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.

输入输出样例

输入样例 #1

5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

输出样例 #1

3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

Input

题意翻译

白面鸮在维护罗德岛的干员数据库。其中有些干员多次改变姓名,白面鸮想知道干员最初的姓名和当前的姓名(干员可多次改名)。请您帮助她。 input 一行n,表示有n次修改操作(1<=n<=1000) 接下来n行,每行2个字符串,长度不过20,以空格相隔。 output 一行,输出有m个干员(输出顺序任意) 接下来m行,每行2个字符串,表示干员最初姓名和当前姓名。

加入题单

算法标签: