Submission #4036160


Source Code Expand

import java.io.*;
import java.util.*;

public class Main {

    static int N, M;
    static Edge[] E;
    static int Q;
    static int[] S, T;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        E = new Edge[M];
        for (int i = 0; i < M; i++) {
            E[i] = new Edge(sc.nextInt()-1, sc.nextInt()-1, sc.nextInt());
        }
        Q = sc.nextInt();
        S = new int[Q];
        T = new int[Q];
        for (int i = 0; i < Q; i++) {
            S[i] = sc.nextInt()-1;
            T[i] = sc.nextInt()-1;
        }

        writeLines(solve());
    }

    static long[] solve() {
        kruskal(N, E);

        long[] ans = new long[Q];
        for (int i = 0; i < Q; i++) {
            ans[i] = calc(S[i], T[i]);
        }
        return ans;
    }

    static long calc(int s, int t) {
        long maxEdge = dfs(-1, s, t, -1);
        return mstCost - maxEdge;
    }

    static long dfs(int p, int a, int t, int maxEdge) {
        if( a == t ) return maxEdge;

        for (Edge e : mst[a]) {
            int b = e.opposite(a);
            if( b == p ) continue;

            long ret = dfs(a, b, t, Math.max(e.c, maxEdge));
            if( ret > 0 ) return ret;
        }
        return -1;
    }

    static List<Edge>[] mst;
    static long mstCost;

    static void kruskal(int N, Edge[] E) {
        Arrays.sort(E, Comparator.comparingInt(e -> e.c));
        List<Edge>[] T = new List[N];
        for (int i = 0; i < N; i++) {
            T[i] = new ArrayList<>();
        }

        long cost = 0;
        Set<Integer> set = new HashSet<>();
        for (Edge e : E) {
            if( set.contains(e.a) && set.contains(e.b) ) continue;

            T[e.a].add(e);
            T[e.b].add(e);
            set.add(e.a);
            set.add(e.b);
            cost += e.c;

            if( set.size() == N ) break;
        }

        mst = T;
        mstCost = cost;
    }

    static class Edge {
        final int a, b, c;

        public Edge(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        int opposite(int x) {
            return a == x ? b : a;
        }
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        int[] nextIntArray(int n, int delta) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt() + delta;
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    static void writeLines(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int a : as) pw.println(a);
        pw.flush();
    }

    static void writeLines(long[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (long a : as) pw.println(a);
        pw.flush();
    }

    static void writeSingleLine(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int i = 0; i < as.length; i++) {
            if (i != 0) pw.print(" ");
            pw.print(as[i]);
        }
        pw.println();
        pw.flush();
    }

    static int max(int... as) {
        int max = Integer.MIN_VALUE;
        for (int a : as) max = Math.max(a, max);
        return max;
    }

    static int min(int... as) {
        int min = Integer.MAX_VALUE;
        for (int a : as) min = Math.min(a, min);
        return min;
    }

    static void debug(Object... args) {
        StringJoiner j = new StringJoiner(" ");
        for (Object arg : args) {
            if (arg == null) j.add("null");
            else if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
            else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
            else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
            else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
            else j.add(arg.toString());
        }
        System.err.println(j.toString());
    }

    static void printSingleLine(int[] array) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int i = 0; i < array.length; i++) {
            if (i != 0) pw.print(" ");
            pw.print(array[i]);
        }
        pw.println();
        pw.flush();
    }

    static int lowerBound(int[] array, int value) {
        int lo = 0, hi = array.length, mid;
        while (lo < hi) {
            mid = (hi + lo) / 2;
            if (array[mid] < value) lo = mid + 1;
            else hi = mid;
        }
        return lo;
    }

    static int upperBound(int[] array, int value) {
        int lo = 0, hi = array.length, mid;
        while (lo < hi) {
            mid = (hi + lo) / 2;
            if (array[mid] <= value) lo = mid + 1;
            else hi = mid;
        }
        return lo;
    }
}

Submission Info

Submission Time
Task A - Graph
User kusomushi
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 6468 Byte
Status WA
Exec Time 1249 ms
Memory 106240 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 0 / 200 0 / 300 0 / 200
Status
AC × 2
AC × 2
WA × 10
AC × 3
WA × 18
AC × 5
WA × 26
Set Name Test Cases
Sample sample_1.txt, sample_2.txt
subtask1 sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
subtask2 sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt
All sample_1.txt, sample_2.txt, sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt
Case Name Status Exec Time Memory
sample_1.txt AC 189 ms 26580 KB
sample_2.txt AC 151 ms 23124 KB
subtask_1_1.txt WA 178 ms 26708 KB
subtask_1_10.txt WA 618 ms 61652 KB
subtask_1_11.txt AC 170 ms 26580 KB
subtask_1_2.txt WA 364 ms 43032 KB
subtask_1_3.txt WA 965 ms 70484 KB
subtask_1_4.txt WA 206 ms 27048 KB
subtask_1_5.txt WA 239 ms 28908 KB
subtask_1_6.txt WA 430 ms 49880 KB
subtask_1_7.txt WA 967 ms 74044 KB
subtask_1_8.txt WA 228 ms 28140 KB
subtask_1_9.txt WA 292 ms 35092 KB
subtask_2_1.txt WA 977 ms 71580 KB
subtask_2_2.txt WA 940 ms 74884 KB
subtask_2_3.txt WA 971 ms 72140 KB
subtask_2_4.txt WA 1014 ms 74472 KB
subtask_2_5.txt WA 257 ms 29168 KB
subtask_2_6.txt WA 329 ms 36096 KB
subtask_2_7.txt WA 459 ms 46552 KB
subtask_2_8.txt WA 1102 ms 75720 KB
subtask_3_1.txt WA 1219 ms 101488 KB
subtask_3_2.txt WA 1067 ms 101496 KB
subtask_3_3.txt WA 455 ms 49308 KB
subtask_3_4.txt WA 456 ms 49888 KB
subtask_3_5.txt WA 864 ms 69336 KB
subtask_3_6.txt WA 980 ms 84144 KB
subtask_3_7.txt WA 1249 ms 106240 KB
subtask_3_8.txt WA 1234 ms 102832 KB