CF847A.Union of Doubly Linked Lists

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.

In this problem you are given nn memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 11 to nn .

For each cell ii you are given two values:

  • lil_{i} — cell containing previous element for the element in the cell ii ;
  • rir_{i} — cell containing next element for the element in the cell ii .

If cell ii contains information about the element which has no previous element then li=0l_{i}=0 . Similarly, if cell ii contains information about the element which has no next element then ri=0r_{i}=0 .

Three lists are shown on the picture.For example, for the picture above the values of ll and rr are the following: l1=4l_{1}=4 , r1=7r_{1}=7 ; l2=5l_{2}=5 , r2=0r_{2}=0 ; l3=0l_{3}=0 , r3=0r_{3}=0 ; l4=6l_{4}=6 , r4=1r_{4}=1 ; l5=0l_{5}=0 , r5=2r_{5}=2 ; l6=0l_{6}=0 , r6=4r_{6}=4 ; l7=1l_{7}=1 , r7=0r_{7}=0 .

Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values lil_{i} , rir_{i} .

Any other action, other than joining the beginning of one list to the end of another, can not be performed.

输入格式

The first line contains a single integer nn ( 1<=n<=1001<=n<=100 ) — the number of memory cells where the doubly linked lists are located.

Each of the following nn lines contains two integers lil_{i} , rir_{i} ( 0<=li,ri<=n0<=l_{i},r_{i}<=n ) — the cells of the previous and the next element of list for cell ii . Value li=0l_{i}=0 if element in cell ii has no previous element in its list. Value ri=0r_{i}=0 if element in cell ii has no next element in its list.

It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of nn given cells.

输出格式

Print nn lines, the ii -th line must contain two integers lil_{i} and rir_{i} — the cells of the previous and the next element of list for cell ii after all lists from the input are united in a single list. If there are many solutions print any of them.

输入输出样例

  • 输入#1

    7
    4 7
    5 0
    0 0
    6 1
    0 2
    0 4
    1 0
    

    输出#1

    4 7
    5 6
    0 5
    6 1
    3 2
    2 4
    1 0
    
首页