Home > Back-end >  [C] list to heavy
[C] list to heavy

Time:10-20

L2-002 list to heavy (25 points)

Given an integer key value chain table L, you need to delete duplicate key values of absolute value of the node, or for each key value K, only the first node of the absolute value is equal to K is retained, at the same time, all the deleted node must be stored on another list, such as a given for 21 L - - 15 - to 15 - to 7-15, you need to output to the list after 21 - to 15 - - 7, and removed the list - 15-15,

Input format:
Input in the first row L of the first node of the given address and a positive integer N (105 or less, for the total number of nodes), the address of a node is minus five integers, NULL with empty address? 1,

Then N lines, each line in the following format describes a node:

Address key values the next node

Which address is the address of the node, the key value is the absolute value of no more than 104 integer, the next node is the address of the next node,

The output format:
Output to heavy chain table first, and then output the deleted list, each node of a line, according to the format of the input output,

Input the sample:
00100
99999-7 87654
23854-15 00000
87654 15-1
00000-15 99999
00100 21 23854

The output sample:
00100 21 23854
23854-15 99999
99999-7-1
00000-15 87654
87654 15-1

The second test point has been a hard, just want to output several kinds of boundary conditions are right, could you tell me what is the real big problem?

 # include & lt; Bits/stdc++. H> 
# define MAXN 1005000
Int the pre [MAXN], val [MAXN], next [MAXN], m [MAXN];
Int main () {
Int head, n;
STD: : cin> head> n;

for(int i=0; iint a,b,c; STD: : cin> A> b> c;
Next [a]=c; Val [a]=b;
If (c> Pre=0) [c]=a;
}

Int another_head=1, another_tail=1;
For (int PTR=head; PTR!=1; PTR=next (PTR)) {
If (m/STD: : abs (val (PTR))]) {
The next [pre [PTR]]=next (PTR).
If (next (PTR) & gt;=0) pre [next [PTR]]=pre (PTR);
If (another_head==1) {
Another_head=PTR;
Another_tail=PTR;
} else {
The next [another_tail]=PTR;
The pre (PTR)=another_tail;
Another_tail=PTR;
}
} the else m [STD: : abs (val (PTR))]=1;
}

If (another_tail & gt;=0)
The next [another_tail]=1;

For (int PTR=head; PTR!=1; PTR=next (PTR)) {
Printf (" % 5 d % d ", PTR, val (PTR));
Next (PTR)==1? Printf (" \ n - 1 ") : printf (" % 5 d \ n ", next (PTR));
}

For (int PTR=another_head; PTR!=1; PTR=next (PTR)) {
Printf (" % 5 d % d ", PTR, val (PTR));
Next (PTR)==1? Printf (" \ n - 1 ") : printf (" % 5 d \ n ", next (PTR));
}

return 0;
}


  • Related