Submission #223829


Source Code Expand

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


public class Main {

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

	void solve() throws Exception {
		FastScanner in = new FastScanner(System.in);

		int N = in.nextInt();

		LinkedList<Integer>[] ch = new LinkedList[N];
		int[] par = new int[N];
		for (int i = 0; i < ch.length; i++) {
			ch[i] = new LinkedList<Integer>();
		}

		for (int i = 1; i < N; i++) {
			par[i] = in.nextInt();
			ch[par[i]].add(i);
		}

		int[] chcnt = new int[N];
		for (int i = N - 1; i >= 0; i--) {
			chcnt[i]++;
			chcnt[par[i]] += chcnt[i];
		}


		//
		//
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < N; i++) {
			int max = 0;
			int sum = 0;
			for (int c : ch[i]) {
				max = Math.max(max, chcnt[c]);
				sum += chcnt[c];
			}
			max = Math.max(max, N - (1 + sum));

			sb.append(max + "\n");
		}

		System.out.print(sb);




	}
	//
	class FastScanner {

		private InputStream _stream;
		private byte[] _buf = new byte[1024];
		private int _curChar;
		private int _numChars;
		private StringBuilder _sb = new StringBuilder();

		FastScanner(InputStream stream) {
			this._stream = stream;
		}

		public int read() {
			if (_numChars == -1) throw new InputMismatchException();
			if (_curChar >= _numChars) {
				_curChar = 0;
				try {
					_numChars = _stream.read(_buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (_numChars <= 0) return -1;
			}
			return _buf[_curChar++];
		}

		public String next() {
			int c = read();
			while (isWhitespace(c)) {
				c = read();
			}
			_sb.setLength(0);
			do {
				_sb.appendCodePoint(c);
				c = read();
			} while (!isWhitespace(c));
			return _sb.toString();
		}

		public int nextInt() {
			return (int) nextLong();
		}
		public long nextLong() {
			int c = read();
			while (isWhitespace(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9') throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isWhitespace(c));
			return res * sgn;
		}

		public boolean isWhitespace(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}
}
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

Submission Info

Submission Time
Task C - 高橋王国の分割統治
User ixxa
Language Java (OpenJDK 1.7.0)
Score 100
Code Size 2518 Byte
Status AC
Exec Time 739 ms
Memory 43316 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
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 2
AC × 11
AC × 16
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
Subtask1 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt
Subtask2 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt
Case Name Status Exec Time Memory
sample_01.txt AC 375 ms 20792 KB
sample_02.txt AC 387 ms 20604 KB
subtask1_01.txt AC 369 ms 20660 KB
subtask1_02.txt AC 390 ms 20780 KB
subtask1_03.txt AC 375 ms 20788 KB
subtask1_04.txt AC 364 ms 20656 KB
subtask1_05.txt AC 370 ms 20920 KB
subtask1_06.txt AC 379 ms 20916 KB
subtask1_07.txt AC 394 ms 20852 KB
subtask1_08.txt AC 374 ms 21032 KB
subtask1_09.txt AC 371 ms 21044 KB
subtask2_01.txt AC 591 ms 38736 KB
subtask2_02.txt AC 641 ms 40252 KB
subtask2_03.txt AC 643 ms 43256 KB
subtask2_04.txt AC 642 ms 43316 KB
subtask2_05.txt AC 599 ms 42192 KB
subtask2_06.txt AC 739 ms 42280 KB
subtask2_07.txt AC 665 ms 43132 KB