CF501B.Misha and Changing Handles

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 qq ( 1<=q<=10001<=q<=1000 ), the number of handle change requests.

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

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

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

输出格式

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

In the next nn lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, oldold and newnew , separated by a space, meaning that before the user had handle oldold , and after all the requests are completed, his handle is newnew . 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
    
首页