CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:andreibe
Submission time:2021-10-08 18:49:19 +0300
Language:Java
Status:COMPILE ERROR

Compiler report

input/Main.java:2: error: package javafx.util does not exist
import javafx.util.Pair;
                  ^
input/Main.java:8: error: cannot find symbol
    static ArrayList<ArrayList<Pair<Integer,Integer>>> tree;
                               ^
  symbol:   class Pair
  location: class Main
input/Main.java:24: error: cannot find symbol
            ArrayList<Pair<Integer,Integer>> neighbors = tree.get(a);
                      ^
  symbol:   class Pair
  location: class Main
input/Main.java:25: error: cannot find symbol
            ArrayList<Pair<Integer,Integer>> neighbors2 = tree.get(b);
                      ^
  symbol:   class Pair
  location: class Main
input/Main.java:26: error: cannot find symbol
            neighbors.add(new Pair<>(b,x));
                              ^
  symbol:   class Pair
  location: class Main
input/Main.java:27: error: cannot find symbol
            neighbors2.add(new Pair<>(a,x));
                               ^
  symbol:   class Pair
  location: class Mai...

Code

import javafx.util.Pair;

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

public class Main {
    static ArrayList<ArrayList<Pair<Integer,Integer>>> 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<Integer,Integer>> neighbors = tree.get(a);
            ArrayList<Pair<Integer,Integer>> 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<Integer,Integer>> naapurit = tree.get(index);
        kayty[index] = true;
        for (Pair<Integer, Integer> naapuri : naapurit) {
            if (!kayty[naapuri.getKey()]) {
                int val = Math.min(min,naapuri.getValue());
                total += val;
                count(naapuri.getKey(),val);
            }
        }
    }
}