CSES - NOI 2019 - Results
Submission details
Task:Distance Code
Sender:Luis Galan
Submission time:2019-03-06 15:49:17 +0200
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.30 s1, 2, 3details
#20.23 s1, 2, 3details
#30.23 s1, 2, 3details
#40.23 s1, 2, 3details
#50.29 s1, 2, 3details
#6ACCEPTED0.29 s1, 2, 3details
#70.23 s1, 2, 3details
#80.23 s1, 2, 3details
#90.24 s1, 2, 3details
#100.23 s1, 2, 3details
#110.22 s1, 2, 3details
#120.30 s2, 3details
#130.28 s2, 3details
#140.28 s2, 3details
#150.28 s2, 3details
#16--3details
#170.81 s3details
#180.82 s3details
#190.81 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 >= 0 && distances[i] == 1 + distances[pIndex]) {
            nodes[pIndex].children.add(nodes[i]);
            System.out.println((i + 1) + " " + (pIndex + 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[a - 1].children.add(nodes[b - 1]);
                nodes[b - 1].parent = nodes[a - 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] = Integer.parseInt(scanner.next());
            }
            
            // run decode with index = n - 1
            decode(distances, nodes, n - 1);
        }

        scanner.close();
    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
2
2 1

correct output
(empty)

user output
1 2

Test 2

Group: 1, 2, 3

Verdict:

input
1
3
3 1
2 1

correct output
(empty)

user output
1
2

Test 3

Group: 1, 2, 3

Verdict:

input
1
4
3 2
2 1
4 1

correct output
(empty)

user output
1
4

Test 4

Group: 1, 2, 3

Verdict:

input
1
4
2 3
3 4
1 3

correct output
(empty)

user output
4
3
1

Test 5

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
4 5
3 4
2 5
1 5

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

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

correct output
(empty)

user output
4 5
3 5
2 5
1 2

Test 7

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
3
2
4
1

Test 8

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
3
7
6
5
4
...

Test 9

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
2
8
6
1
7
...

Test 10

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
5
2
6
7
1
...

Test 11

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
8
6
2
1

Test 12

Group: 2, 3

Verdict:

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

correct output
(empty)

user output
255
428
108
238
437
...
Truncated

Test 13

Group: 2, 3

Verdict:

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

correct output
(empty)

user output
243
47
1

Test 14

Group: 2, 3

Verdict:

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

correct output
(empty)

user output
20
217
1

Test 15

Group: 2, 3

Verdict:

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

correct output
(empty)

user output
270
70
218
488
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
85047
85005
1

Test 18

Group: 3

Verdict:

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

correct output
(empty)

user output
60473
41037
1

Test 19

Group: 3

Verdict:

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

correct output
(empty)

user output
30351
50013
1

Test 20

Group: 1, 2, 3

Verdict:

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

correct output
(empty)

user output
1
2
4