結果

問題 No.1817 Reversed Edges
ユーザー ks2mks2m
提出日時 2022-01-21 22:43:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 729 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 2,526 ms
コンパイル使用メモリ 74,632 KB
実行使用メモリ 86,792 KB
最終ジャッジ日時 2023-08-17 07:02:48
合計ジャッジ時間 14,811 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
49,412 KB
testcase_01 AC 40 ms
49,404 KB
testcase_02 AC 42 ms
49,364 KB
testcase_03 AC 39 ms
49,420 KB
testcase_04 AC 40 ms
49,816 KB
testcase_05 AC 41 ms
49,404 KB
testcase_06 AC 41 ms
49,352 KB
testcase_07 AC 572 ms
70,636 KB
testcase_08 AC 325 ms
62,132 KB
testcase_09 AC 578 ms
70,092 KB
testcase_10 AC 348 ms
62,240 KB
testcase_11 AC 404 ms
64,256 KB
testcase_12 AC 693 ms
74,352 KB
testcase_13 AC 690 ms
74,528 KB
testcase_14 AC 729 ms
74,740 KB
testcase_15 AC 705 ms
74,884 KB
testcase_16 AC 714 ms
74,884 KB
testcase_17 AC 671 ms
74,580 KB
testcase_18 AC 676 ms
75,012 KB
testcase_19 AC 673 ms
74,508 KB
testcase_20 AC 717 ms
74,756 KB
testcase_21 AC 683 ms
74,672 KB
testcase_22 AC 519 ms
74,228 KB
testcase_23 AC 523 ms
74,272 KB
testcase_24 AC 515 ms
86,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static int n;
	static List<List<Integer>> list;
	static int[] ans;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		n = Integer.parseInt(sa[0]);
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			list.get(a).add(b);
			list.get(b).add(a);
		}
		br.close();

		ans = new int[n];
		ans[0] = dfs(0, -1);
		dfs2(0, -1);

		PrintWriter pw = new PrintWriter(System.out);
		for (int i : ans) {
			pw.println(i);
		}
		pw.flush();
	}

	static int dfs(int x, int p) {
		int ret = 0;
		for (int i : list.get(x)) {
			if (i != p) {
				if (x > i) {
					ret++;
				}
				int res = dfs(i, x);
				ret += res;
			}
		}
		return ret;
	}

	static void dfs2(int x, int p) {
		for (int i : list.get(x)) {
			if (i != p) {
				if (x < i) {
					ans[i] = ans[x] + 1;
				} else {
					ans[i] = ans[x] - 1;
				}
				dfs2(i, x);
			}
		}
	}
}
0