| Task: | Tietoverkko |
| Sender: | andreibe |
| Submission time: | 2021-10-08 18:52:41 +0300 |
| Language: | Java |
| Status: | READY |
| Result: | 10 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 10 |
| #2 | TIME LIMIT EXCEEDED | 0 |
| #3 | TIME LIMIT EXCEEDED | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.17 s | 1, 2, 3 | details |
| #2 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
| #3 | TIME LIMIT EXCEEDED | -- | 3 | details |
Code
import java.util.ArrayList;
import java.util.Scanner;
class Pair {
public final int key;
public final int value;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
}
public class Main {
static ArrayList<ArrayList<Pair>> tree;
static int total;
static boolean[] kayty;
static int n;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
n = input.nextInt();
tree = new ArrayList<>(n+1);
for (int i = 0; i < n+1; i++) {
tree.add(new ArrayList<>());
}
kayty = new boolean[n+1];
for (int i = 0; i < n-1; i++) {
int a = Integer.parseInt(input.next());
int b = Integer.parseInt(input.next());
int x = Integer.parseInt(input.next());
ArrayList<Pair> neighbors = tree.get(a);
ArrayList<Pair> neighbors2 = tree.get(b);
neighbors.add(new Pair(b,x));
neighbors2.add(new Pair(a,x));
}
for (int i = 0; i < tree.size(); i++) {
kayty = new boolean[n+1];
kayty[i] = true;
count(i,Integer.MAX_VALUE);
}
System.out.println(total/2);
}
private static void count(int index, int min) {
ArrayList<Pair> naapurit = tree.get(index);
kayty[index] = true;
for (Pair naapuri : naapurit) {
if (!kayty[naapuri.key]) {
int val = Math.min(min,naapuri.value);
total += val;
count(naapuri.key,val);
}
}
}
}
Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1 2 74 1 3 100 2 4 50 3 5 40 ... |
| correct output |
|---|
| 88687 |
| user output |
|---|
| 88687 |
Test 2
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 5000 1 2 613084013 1 3 832364259 2 4 411999902 3 5 989696303 ... |
| correct output |
|---|
| 1103702320243776 |
| user output |
|---|
| (empty) |
Test 3
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 200000 1 2 613084013 1 3 832364259 2 4 411999902 3 5 989696303 ... |
| correct output |
|---|
| 1080549209850010931 |
| user output |
|---|
| (empty) |
