CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:andreibe
Submission time:2021-10-08 21:54:25 +0300
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.16 s1, 2, 3details
#20.74 s2, 3details
#3--3details

Code


import java.util.ArrayList;
import java.util.Scanner;
class Pair {
    public int key;
    public int value;

    public Pair(int key, int value) {
        this.key = key;
        this.value = value;
    }
}

public class Main {
    static ArrayList<ArrayList<Pair>> tree;
    static int n;
    static Pair[] paths;
    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<>());
        }
        paths = new Pair[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);
            neighbors.add(new Pair(b,x));
        }
        count(1,1,Integer.MAX_VALUE);
        long total = 0;
        for (Pair path : paths) {
            if (path != null) {
                total += path.value;
            }
        }
        System.out.println(total);
    }
    private static final ArrayList<Pair> added = new ArrayList<>();
    private static final ArrayList<Integer> before = new ArrayList<>();

    private static void count(int index, int lastIndex,int paino) {
        if (lastIndex == 0) {
            return;
        }
        ArrayList<Pair> naapurit = tree.get(index);
        paths[index] = new Pair(Integer.MAX_VALUE,0);
        added.add(paths[index]);
        before.add(paino);
        boolean b=false;
        for (Pair naapuri : naapurit) {
            Pair pair = paths[naapuri.key]; //onko käyty ennen
            if (pair == null) { //Ei olla käyty ennen
                b=true;
                for (int i = 0; i < added.size(); i++) {
                    Pair path = added.get(i);
                    before.set(i,path.key);
                    path.key = Math.min(path.key,naapuri.value);
                    path.value += path.key;
                }
                count(naapuri.key,index,naapuri.value);
            }
        }
        if (!b) {
            for (int i = 0; i < added.size(); i++) {
                added.get(i).key = before.get(i);
            }
        }
    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100
1 2 74
1 3 100
2 4 50
3 5 40
...

correct output
88687

user output
39120

Test 2

Group: 2, 3

Verdict:

input
5000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1103702320243776

user output
68791006761

Test 3

Group: 3

Verdict:

input
200000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1080549209850010931

user output
(empty)