您的位置:首页 > 其它

[Codeforces] 847A - Union of Doubly Linked Lists - 搜索

2017-11-03 10:40 2581 查看
A. Union of Doubly Linked Lists

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 n memory cells forming one or more doubly linked lists. Each cell contain
4000
s information about element
from some list. Memory cells are numbered from 1 to n.

For each cell i you are given two values:

li —
cell containing previous element for the element in the cell i;

ri —
cell containing next element for the element in the cell i.

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


Three
lists are shown on the picture.

For example, for the picture above the values of l and r are
the following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 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 li, ri.

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

Input

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

Each of the following n lines contains two integers li, ri (0 ≤ li, ri ≤ n)
— the cells of the previous and the next element of list for cell i. Value li = 0 if
element in cell i has no previous element in its list. Value ri = 0 if
element in cell i 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 n given
cells.

Output

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

Example

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


output
4 7
5 6
0 5
6 1
3 2
2 4
1 0


   
给你一个破碎(?)的链表
首先是一个数字 n 表述链表节点的个数,从1 -> n 进行编号
接下来 n 行数据
每行两个数组 x, y
第 i 行表示 i 号结点左边是 x 结点,右边是 y 结点。如果是 0 表示无结点
问要把这些破碎的链表组成一个完整的链表
需要怎么修改
按照输入的格式
输出修改后的完整的链表

直接储存所有 x 是 0 的节点编号(即左边无元素),然后任意选一个开始爆搜,搜到 y 是 0 终止(即右边无元素),然后再取一个 x 是 0 的节点
拼接到之前终止的x 是 0 的节点。再开始新一轮爆搜,直到没有 x 是 0 的结点为止

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

struct Node
{
int a;
int b;
int c;
};

Node node
;
int n;
queue<int> Q;

int dfs(int x)
{
if(node[x].c == 0){
return x;
}
return dfs(node[x].c);
}

int main()
{
int mark;
int ne;

while(scanf("%d", &n) == 1){
mark = 1;
while(!Q.empty()){
Q.pop();
}
for(int i = 1; i <= n; i ++){
scanf("%d%d", &node[i].a, &node[i].c);
node[i].b = i;
if(!node[i].a){
Q.push(i);
}
}
while(!Q.empty()){
int now;

now = Q.front();
Q.pop();
if(!mark){
node[now].a = ne;
node[ne].c = now;
}
mark = 0;
ne = dfs(now);
}
for(int i = 1; i <= n; i ++){
printf("%d %d\n", node[i].a, node[i].c);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: