CSES - NOI 2019 - Results
Submission details
Task:Distance Code
Sender:Luis Galan
Submission time:2019-03-06 16:22:10 +0200
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.29 s1, 2, 3details
#20.23 s1, 2, 3details
#3ACCEPTED0.31 s1, 2, 3details
#40.23 s1, 2, 3details
#50.23 s1, 2, 3details
#60.24 s1, 2, 3details
#70.23 s1, 2, 3details
#80.23 s1, 2, 3details
#90.23 s1, 2, 3details
#100.23 s1, 2, 3details
#110.23 s1, 2, 3details
#120.28 s2, 3details
#130.29 s2, 3details
#140.29 s2, 3details
#150.28 s2, 3details
#16--3details
#170.81 s3details
#180.81 s3details
#190.80 s3details
#200.23 s1, 2, 3details

Code

import java.util.Scanner;
import java.util.ArrayList;

public class distance {

    public static class Node {
        ArrayList<Node> children;
        Node parent;
        int id;

        public Node(int id) {
            children = new ArrayList<>();
            this.id = id;
        }
    }

    public static void encode(Node root) {
        // remove nodes depth-first
        for (int i = 0; i < root.children.size(); i++) {
            encode(root.children.get(i));
        }
        System.out.println(root.id + 1);
    }

    public static int decode(int[] distances, Node[] nodes, int pIndex) {
        // loop through nodes that belong to parent
        int i = pIndex + 1;
        while (i < distances.length && distances[i] == 1 + distances[pIndex]) {
            nodes[pIndex].children.add(nodes[i]);
            System.out.println((nodes[i].id + 1) + " " + (nodes[pIndex].id + 1));
            i = decode(distances, nodes, i);
        }
        return i;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.next());
        int n = Integer.parseInt(scanner.next());

        Node[] nodes = new Node[n];
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new Node(i);
        }

        if (t == 1) { // encode
            for (int i = 0; i < n - 1; i++) {
                int a = Integer.parseInt(scanner.next());
                int b = Integer.parseInt(scanner.next());
                nodes[b - 1].children.add(nodes[a - 1]);
                nodes[a - 1].parent = nodes[b - 1];
            }
            Node root = nodes[0];
            while (root.parent != null) {
                root = root.parent;
            }
            encode(root);
        } else { // decode
            // get distances
            int[] distances = new int[n + 1]; // add 0 as last element
            for (int i = 0; i < n - 1; i++) {
                distances[i + 1] = Integer.parseInt(scanner.next());
            }
            
            // run decode with index = n - 1
            decode(distances, nodes, 0);
        }

        scanner.close();
    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
2
2 1

correct output
(empty)

user output
2 1

Test 2

Group: 1, 2, 3

Verdict:

input
1
3
3 1
2 1

correct output
(empty)

user output
(empty)

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
4
3 2
2 1
4 1

correct output
(empty)

user output
2 1
3 2
4 1

Test 4

Group: 1, 2, 3

Verdict:

input
1
4
2 3
3 4
1 3

correct output
(empty)

user output
(empty)

Test 5

Group: 1, 2, 3

Verdict:

input
1
5
3 5
4 1
1 3
...

correct output
(empty)

user output
4
1
3
5

Test 6

Group: 1, 2, 3

Verdict:

input
1
5
3 2
3 4
5 1
...

correct output
(empty)

user output
5
1
3
4

Test 7

Group: 1, 2, 3

Verdict:

input
1
5
4 3
1 4
4 2
...

correct output
(empty)

user output
1
5
4
2

Test 8

Group: 1, 2, 3

Verdict:

input
1
10
9 3
8 9
2 9
...

correct output
(empty)

user output
8
2
1
10
9
...

Test 9

Group: 1, 2, 3

Verdict:

input
1
10
9 2
5 8
7 1
...

correct output
(empty)

user output
5
9
7
1
6
...

Test 10

Group: 1, 2, 3

Verdict:

input
1
10
10 4
9 1
4 7
...

correct output
(empty)

user output
9
3
8
10
4
...

Test 11

Group: 1, 2, 3

Verdict:

input
1
10
2 6
4 3
3 5
...

correct output
(empty)

user output
9
1
2
10
6

Test 12

Group: 2, 3

Verdict:

input
1
500
10 6
6 255
6 428
...

correct output
(empty)

user output
10
7
65
157
326
...
Truncated

Test 13

Group: 2, 3

Verdict:

input
1
500
152 466
451 313
158 479
...

correct output
(empty)

user output
335
1
47

Test 14

Group: 2, 3

Verdict:

input
1
500
109 440
330 190
443 161
...

correct output
(empty)

user output
197
250
216
273
340
...

Test 15

Group: 2, 3

Verdict:

input
1
500
144 373
257 233
341 318
...

correct output
(empty)

user output
184
1
79
393
405

Test 16

Group: 3

Verdict:

input
1
100000
54983 75172
93807 75172
44082 75172
...

correct output
(empty)

user output
(empty)

Test 17

Group: 3

Verdict:

input
1
100000
88863 19059
86423 76688
98536 95984
...

correct output
(empty)

user output
36494
88117
73221
1
85005

Test 18

Group: 3

Verdict:

input
1
100000
59979 6389
19097 24999
27846 82330
...

correct output
(empty)

user output
1
72435
91
84498
62411
...

Test 19

Group: 3

Verdict:

input
1
100000
58761 66001
25102 51081
98625 67861
...

correct output
(empty)

user output
1
50013
9592
27295
30351

Test 20

Group: 1, 2, 3

Verdict:

input
1
6
2 1
3 2
4 2
...

correct output
(empty)

user output
(empty)