Submission #867078


Source Code Expand

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

public class Main {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;
	
	List<Integer>[] g;
	int[] b;
	int k;
	
	int dfs(int v, int p) {
		int depth = 0;
		for (int u : g[v]) {
			if (u == p) {
				continue;
			}
			depth = Math.max(depth, dfs(u, v) + 1);
		}
		
		if (depth == k - 1) {
			b[v] = 0;
			return -1;
		}
		return depth;
	}

	void solve() throws IOException {
		int n = nextInt();
		k = nextInt();
		int[] a = new int[n];
		
		for (int i = 0; i < n; i++) {
			a[i] = nextInt() - 1;
		}
		
		b = a.clone();
		b[0] = 0;
		
		g = new List[n];
		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<>();
		}
		
		for (int i = 1; i < n; i++) {
			g[b[i]].add(i);
		}
		
		dfs(0, -1);
		
		int ret = 0;
		for (int i = 0; i < n; i++) {
			if (a[i] != b[i]) {
				ret++;
			}
		}
		
		out.println(ret);
	}

	Main() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		
		Thread t = new Thread(null, new Runnable() {

			@Override
			public void run() {
				try {
					solve();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}, "lul", 1 << 26);
		
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}

Submission Info

Submission Time
Task D - Teleporter
User mmaxio
Language Java8 (OpenJDK 1.8.0)
Score 800
Code Size 2203 Byte
Status AC
Exec Time 757 ms
Memory 44128 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 800 / 800
Status
AC × 3
AC × 58
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 152 ms 7952 KB
0_01.txt AC 152 ms 8056 KB
0_02.txt AC 152 ms 7924 KB
1_00.txt AC 152 ms 8056 KB
1_01.txt AC 156 ms 7928 KB
1_02.txt AC 404 ms 44108 KB
1_03.txt AC 757 ms 41584 KB
1_04.txt AC 412 ms 44128 KB
1_05.txt AC 416 ms 43064 KB
1_06.txt AC 413 ms 44124 KB
1_07.txt AC 413 ms 43004 KB
1_08.txt AC 400 ms 43020 KB
1_09.txt AC 404 ms 43036 KB
1_10.txt AC 405 ms 39256 KB
1_11.txt AC 404 ms 38604 KB
1_12.txt AC 404 ms 38600 KB
1_13.txt AC 364 ms 31712 KB
1_14.txt AC 380 ms 30820 KB
1_15.txt AC 428 ms 30732 KB
1_16.txt AC 328 ms 26348 KB
1_17.txt AC 336 ms 26380 KB
1_18.txt AC 352 ms 27148 KB
1_19.txt AC 304 ms 24852 KB
1_20.txt AC 313 ms 24884 KB
1_21.txt AC 316 ms 24852 KB
1_22.txt AC 344 ms 25316 KB
1_23.txt AC 336 ms 24272 KB
1_24.txt AC 332 ms 24296 KB
1_25.txt AC 332 ms 24200 KB
1_26.txt AC 324 ms 24224 KB
1_27.txt AC 329 ms 25304 KB
1_28.txt AC 317 ms 24028 KB
1_29.txt AC 312 ms 23988 KB
1_30.txt AC 320 ms 25040 KB
1_31.txt AC 320 ms 24280 KB
1_32.txt AC 325 ms 24260 KB
1_33.txt AC 332 ms 25312 KB
1_34.txt AC 336 ms 24356 KB
1_35.txt AC 332 ms 24224 KB
1_36.txt AC 332 ms 24284 KB
1_37.txt AC 332 ms 24276 KB
1_38.txt AC 332 ms 24288 KB
1_39.txt AC 328 ms 24348 KB
1_40.txt AC 329 ms 24336 KB
1_41.txt AC 329 ms 25292 KB
1_42.txt AC 328 ms 25288 KB
1_43.txt AC 336 ms 25792 KB
1_44.txt AC 333 ms 25664 KB
1_45.txt AC 325 ms 24860 KB
1_46.txt AC 331 ms 25664 KB
1_47.txt AC 357 ms 30052 KB
1_48.txt AC 369 ms 32836 KB
1_49.txt AC 368 ms 33096 KB
1_50.txt AC 372 ms 32516 KB
1_51.txt AC 413 ms 42368 KB
1_52.txt AC 413 ms 42600 KB
1_53.txt AC 412 ms 42212 KB
1_54.txt AC 440 ms 42236 KB