A - Union of Doubly Linked Lists CodeForces - 847A(并查集)
2017-11-22 11:43
405 查看
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 contains 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 gua bc78 ranteed 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个数前面的数和后面的数,然后根据前后关系把他们连成一条链,如果有多个链则合并成一条。在合并过程中使用并查集很方便(使用并查集根据他们前面 的数连成链)。合并的条件为这个数的l = 0,则为链首,可以合并在r = 0的后面(即链尾)
#include<stdio.h> #include<stdlib.h> #include<string.h> struct Node { int front; int next; } a[104]; int f[110]; void init(int n) { for(int i = 1; i <= n; i++) { f[i] = i; } return; } int getf(int v) { if(v == f[v])///当f[v] = v的时候说明他是链首,是头领。 return v; else///若不是则递归寻找 { f[v] = getf(f[v]); return f[v]; } } int merge_f(int x, int y)///相同头领的进行归并 { int t1 = getf(x); int t2 = getf(y); if(t1 != t2) { f[t2] = t1; return 1; } else { return 0; } } int main() { int n, i, j; while(~scanf("%d", &n)) { init(n); for(i = 1; i <= n; i++) { scanf("%d %d", &a[i].front, &a[i].next); if(a[i].front != 0) f[i] = a[i].front;///i的前一个数为他的头领,记在f[i]里 } for(i = 1; i <= n; i++) { if(a[i].next != 0) continue; for(j = 1; j <= n; j++) { if(i != j && a[j].front == 0) { if(merge_f(i, j)) { a[i].next = j; a[j].front = i; break; } } } } for(i = 1; i <= n; i++) { printf("%d %d\n", a[i].front, a[i].next); } } return 0; }
相关文章推荐
- [Codeforces] 847A - Union of Doubly Linked Lists - 搜索
- A. Union of Doubly Linked Lists
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest Union of Doubly Linked Lists
- Educational Codeforces Round 4 D. The Union of k-Segments(★)
- codeforces D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- Educational Codeforces Round 4-D. The Union of k-Segments
- [LeedCode OJ]#160 Intersection of Two Linked Lists
- Educational Codeforces Round 4 D. The Union of k-Segments 排序,思维
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
- Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree(并查集)
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- Intersection of Two Linked Lists
- 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
- LeetCode-Intersection of Two Linked Lists
- LeetCode Intersection of Two Linked Lists
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies