| Task: | Distance Code |
| Sender: | William Bille Meyling |
| Submission time: | 2019-03-07 01:21:47 +0200 |
| Language: | Java |
| Status: | READY |
| Result: | 68 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 21 |
| #2 | ACCEPTED | 47 |
| #3 | TIME LIMIT EXCEEDED | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.25 s | 1, 2, 3 | details |
| #2 | ACCEPTED | 0.25 s | 1, 2, 3 | details |
| #3 | ACCEPTED | 0.26 s | 1, 2, 3 | details |
| #4 | ACCEPTED | 0.23 s | 1, 2, 3 | details |
| #5 | ACCEPTED | 0.26 s | 1, 2, 3 | details |
| #6 | ACCEPTED | 0.24 s | 1, 2, 3 | details |
| #7 | ACCEPTED | 0.24 s | 1, 2, 3 | details |
| #8 | ACCEPTED | 0.25 s | 1, 2, 3 | details |
| #9 | ACCEPTED | 0.26 s | 1, 2, 3 | details |
| #10 | ACCEPTED | 0.26 s | 1, 2, 3 | details |
| #11 | ACCEPTED | 0.26 s | 1, 2, 3 | details |
| #12 | ACCEPTED | 0.28 s | 2, 3 | details |
| #13 | ACCEPTED | 0.29 s | 2, 3 | details |
| #14 | ACCEPTED | 0.30 s | 2, 3 | details |
| #15 | ACCEPTED | 0.30 s | 2, 3 | details |
| #16 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #17 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #18 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #19 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #20 | ACCEPTED | 0.25 s | 1, 2, 3 | details |
Code
/*
NAME: William Bille Meyling
EVENT: NOI 2019
DATE: 6/3-2019
*/
/*Initial thoughts
thinking depth first with iterative deepening maybe (though slow)
using heap for encode?
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
public class Distance_Code_Done {
private static boolean[] visited;
//could merge these classes or extend from another.
private static class Node{
int number;
HashSet<Node> nbs = new HashSet<>();
Node parent = null;
public Node(int number){
this.number = number;
}
}
private static class Node2{
int number;
ArrayList<Node2> node_child = new ArrayList<>();
Node2 parent;
public Node2(int number){
this.number = number;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int type = Integer.valueOf(br.readLine());
int N = Integer.valueOf(br.readLine());
if(type == 1){
ArrayList<Node> nodes = new ArrayList<>();
for(int i = 0; i < N; i++) nodes.add(null);
for(int i = 0; i < N - 1; i++){
String[] input = br.readLine().split(" ");
int a = Integer.valueOf(input[0])-1;
int b = Integer.valueOf(input[1])-1;
if(nodes.get(a) == null){
nodes.set(a, new Node(a));
}
if(nodes.get(b) == null) {
nodes.set(b, new Node(b));
}
nodes.get(a).nbs.add(nodes.get(b));
nodes.get(b).nbs.add(nodes.get(a));
}
Node start = nodes.get(0);
visited = new boolean[N+1];
visited[start.number] = true;
build(start);
dfs_enh(start);
}else{
inputs = br.readLine().split(" ");
Node2 startNode = new Node2(nextNumber);
decode(startNode);
}
}
private static void dfs_enh(Node node){
if(node.nbs.size() == 0){
System.out.print((node.number+1) + " ");
node.parent.nbs.remove(node);
return;
}
ArrayList<Node> children = new ArrayList<>(node.nbs);
for(int i = children.size()-1; i > -1; i--){
dfs_enh(children.get(i));
}
System.out.print((node.number+1) + " ");
}
private static void build(Node node){
node.nbs.remove(node.parent);
for(Node n : node.nbs){
n.parent = node;
build(n);
}
}
private static int nextNumber = 1;
private static String[] inputs;
private static void decode(Node2 node){
Node2 current = node;
for(int i = 0; i < inputs.length; i++){
//micro optimization: save inputs[i] value as int before
int diff = Integer.valueOf(inputs[i]);
if(current.parent == null){
nextNumber++;
current.parent = new Node2(nextNumber);
current.parent.node_child.add(current);
}
current = current.parent;
for(int j = 1; j < diff; j++){
nextNumber++;
Node2 newNode = new Node2(nextNumber);
newNode.parent = current;
current.node_child.add(newNode);
current = newNode;
}
}
traverse(current);
}
private static void traverse(Node2 node){
for(int i = 0; i < node.node_child.size(); i++){
System.out.println(node.number + " " + node.node_child.get(i).number);
traverse(node.node_child.get(i));
}
}
}
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: ACCEPTED
| input |
|---|
| 1 3 3 1 2 1 |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 2 1 2 3 |
Test 3
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 4 3 2 2 1 4 1 |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 3 2 2 1 3 4 |
Test 4
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 4 2 3 3 4 1 3 |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 4 2 2 1 2 3 |
Test 5
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 5 3 5 4 1 1 3 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 3 2 2 1 3 4 4 5 |
Test 6
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 5 3 2 3 4 5 1 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 2 1 2 3 3 4 3 5 |
Test 7
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 5 4 3 1 4 4 2 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 5 2 2 1 2 3 2 4 |
Test 8
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 10 9 3 8 9 2 9 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 10 2 2 1 2 3 2 4 2 5 ... |
Test 9
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 10 9 2 5 8 7 1 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 4 3 3 2 2 1 4 5 5 6 ... |
Test 10
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 10 10 4 9 1 4 7 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 7 5 5 4 4 3 3 2 2 1 ... |
Test 11
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 10 2 6 4 3 3 5 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 6 5 5 4 4 3 3 2 2 1 ... |
Test 12
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 500 10 6 6 255 6 428 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 500 2 2 1 2 3 2 4 2 5 ... Truncated |
Test 13
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 500 152 466 451 313 158 479 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 351 350 350 349 349 348 348 347 347 346 ... Truncated |
Test 14
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 500 109 440 330 190 443 161 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 2 1 2 3 3 4 4 5 5 6 ... Truncated |
Test 15
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 500 144 373 257 233 341 318 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 494 493 493 492 492 491 491 490 490 489 ... Truncated |
Test 16
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1 100000 54983 75172 93807 75172 44082 75172 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| (empty) |
Test 17
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1 100000 88863 19059 86423 76688 98536 95984 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| (empty) |
Test 18
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1 100000 59979 6389 19097 24999 27846 82330 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| (empty) |
Test 19
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1 100000 58761 66001 25102 51081 98625 67861 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| (empty) |
Test 20
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 6 2 1 3 2 4 2 ... |
| correct output |
|---|
| (empty) |
| user output |
|---|
| 6 4 4 2 2 1 2 3 4 5 |
