Submission #869764


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Denis Nedelyaev
 */
public class Main {
    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AtD solver = new AtD(in, out);
        solver.solve(1);
        out.close();
    }

    static class AtD {
        private final FastScanner in;
        private final PrintWriter out;
        private List<Integer>[] children;
        private int maxHeight;
        private int ans = 0;
        private int[] parent;

        public AtD(FastScanner in, PrintWriter out) {
            this.in = in;
            this.out = out;
        }

        public void solve(int testNumber) throws InterruptedException {
            Thread thread = new Thread(null, this::solve, "solution", 1 << 24);
            thread.setUncaughtExceptionHandler((t, e) -> {
                throw new RuntimeException(e);
            });
            thread.start();
            thread.join();
        }

        private void solve() {
            int n = in.nextInt();
            maxHeight = in.nextInt();
            parent = new int[n];

            for (int i = 0; i < n; i++) {
                parent[i] = in.nextInt() - 1;
            }

            if (parent[0] != 0) {
                parent[0] = 0;
                ans++;
            }

            children = new List[n];
            for (int i = 0; i < n; i++) {
                children[i] = new ArrayList<>();
            }
            for (int i = 1; i < n; i++) {
                children[parent[i]].add(i);
            }

            calc(0);

            out.println(ans);
        }

        private int calc(int v) {
            int depth = 0;
            for (int u : children[v]) {
                depth = Math.max(depth, 1 + calc(u));
            }

            if (parent[v] != 0 && depth + 1 == maxHeight) {
                ans++;
                return -1;
            }

            return depth;
        }

    }

    static class FastScanner {
        private final InputStream in;
        private byte[] buffer = new byte[4096];
        private int pos = 0;
        private int size;

        public FastScanner(InputStream inputStream) throws IOException {
            in = inputStream;
            size = 0;
        }

        public int nextInt() {
            int c = skipWhitespace();

            int ans = 0;

            while (c > ' ') {
                ans *= 10;
                ans += c - '0';
                c = nextChar();
            }

            return ans;
        }

        private int skipWhitespace() {
            while (true) {
                int c = nextChar();
                if (c > ' ' || c == -1) {
                    return c;
                }
            }
        }

        private int nextChar() {
            if (pos >= size) {
                try {
                    size = in.read(buffer);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (size <= 0) {
                    return -1;
                }
                pos = 0;
            }
            return buffer[pos++];
        }

    }
}

Submission Info

Submission Time
Task D - Teleporter
User dened
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 3733 Byte
Status TLE
Exec Time 1062 ms
Memory 35276 KB

Compile Error

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

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 800
Status
AC × 3
AC × 50
TLE × 8
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt, 1_54.txt
Case Name Status Exec Time Memory
0_00.txt AC 267 ms 13444 KB
0_01.txt AC 272 ms 13324 KB
0_02.txt AC 275 ms 13380 KB
1_00.txt AC 262 ms 13196 KB
1_01.txt AC 271 ms 13008 KB
1_02.txt TLE 1058 ms 31828 KB
1_03.txt TLE 1062 ms 32332 KB
1_04.txt TLE 1062 ms 32968 KB
1_05.txt TLE 1053 ms 33876 KB
1_06.txt TLE 1062 ms 32200 KB
1_07.txt TLE 1047 ms 33712 KB
1_08.txt TLE 1060 ms 33204 KB
1_09.txt TLE 1062 ms 32204 KB
1_10.txt AC 413 ms 31036 KB
1_11.txt AC 660 ms 31032 KB
1_12.txt AC 629 ms 31300 KB
1_13.txt AC 364 ms 25556 KB
1_14.txt AC 359 ms 25548 KB
1_15.txt AC 388 ms 29872 KB
1_16.txt AC 367 ms 25416 KB
1_17.txt AC 367 ms 25308 KB
1_18.txt AC 373 ms 25296 KB
1_19.txt AC 367 ms 24792 KB
1_20.txt AC 348 ms 24708 KB
1_21.txt AC 353 ms 24660 KB
1_22.txt AC 377 ms 25556 KB
1_23.txt AC 367 ms 25684 KB
1_24.txt AC 370 ms 25548 KB
1_25.txt AC 370 ms 25568 KB
1_26.txt AC 368 ms 25544 KB
1_27.txt AC 354 ms 25552 KB
1_28.txt AC 366 ms 24776 KB
1_29.txt AC 361 ms 24836 KB
1_30.txt AC 364 ms 24708 KB
1_31.txt AC 380 ms 25636 KB
1_32.txt AC 379 ms 25936 KB
1_33.txt AC 360 ms 25432 KB
1_34.txt AC 364 ms 25568 KB
1_35.txt AC 365 ms 25944 KB
1_36.txt AC 372 ms 26056 KB
1_37.txt AC 356 ms 25636 KB
1_38.txt AC 401 ms 25988 KB
1_39.txt AC 387 ms 25760 KB
1_40.txt AC 363 ms 25548 KB
1_41.txt AC 388 ms 25764 KB
1_42.txt AC 384 ms 25808 KB
1_43.txt AC 377 ms 26064 KB
1_44.txt AC 363 ms 26144 KB
1_45.txt AC 367 ms 26444 KB
1_46.txt AC 384 ms 26060 KB
1_47.txt AC 383 ms 28620 KB
1_48.txt AC 372 ms 28620 KB
1_49.txt AC 376 ms 28568 KB
1_50.txt AC 371 ms 28612 KB
1_51.txt AC 371 ms 33700 KB
1_52.txt AC 410 ms 35276 KB
1_53.txt AC 395 ms 34708 KB
1_54.txt AC 373 ms 33956 KB