結果

問題 No.1817 Reversed Edges
ユーザー merlinmerlin
提出日時 2022-01-21 22:20:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 681 ms / 2,000 ms
コード長 1,293 bytes
コンパイル時間 3,096 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 75,356 KB
最終ジャッジ日時 2023-08-17 06:29:38
合計ジャッジ時間 16,046 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
34,668 KB
testcase_01 AC 55 ms
34,320 KB
testcase_02 AC 57 ms
34,552 KB
testcase_03 AC 59 ms
34,888 KB
testcase_04 AC 56 ms
34,536 KB
testcase_05 AC 56 ms
34,648 KB
testcase_06 AC 56 ms
34,736 KB
testcase_07 AC 607 ms
59,348 KB
testcase_08 AC 351 ms
49,492 KB
testcase_09 AC 589 ms
57,184 KB
testcase_10 AC 362 ms
49,252 KB
testcase_11 AC 454 ms
51,280 KB
testcase_12 AC 605 ms
60,668 KB
testcase_13 AC 646 ms
61,156 KB
testcase_14 AC 632 ms
60,628 KB
testcase_15 AC 623 ms
61,016 KB
testcase_16 AC 621 ms
60,440 KB
testcase_17 AC 663 ms
60,864 KB
testcase_18 AC 625 ms
61,772 KB
testcase_19 AC 622 ms
59,836 KB
testcase_20 AC 628 ms
60,580 KB
testcase_21 AC 681 ms
62,016 KB
testcase_22 AC 509 ms
65,484 KB
testcase_23 AC 558 ms
61,168 KB
testcase_24 AC 588 ms
75,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int n=Integer.parseInt(bu.readLine());
        int i,c[]=new int[n],e[]=new int[n];
        ArrayList<Integer> g[]=new ArrayList[n];

        for(i=0;i<n;i++) g[i]=new ArrayList<>();
        for(i=0;i<n-1;i++)
        {
            String s[]=bu.readLine().split(" ");
            int u=Integer.parseInt(s[0])-1,v=Integer.parseInt(s[1])-1;
            g[u].add(v);
            g[v].add(u);
        }

        dfs(g,c,0,-1);
        reroot(g,c,e,0,-1);
        for(i=0;i<n;i++) sb.append(c[i]+e[i]+"\n");
        System.out.print(sb);
    }

    static void dfs(ArrayList<Integer> g[],int c[],int n,int p)
    {
        for(int x:g[n])
        if(x!=p)
        {
            dfs(g,c,x,n);
            c[n]+=c[x];
            if(x<n) c[n]++;
        }
    }

    static void reroot(ArrayList<Integer> g[],int c[],int e[],int n,int p)
    {
        for(int x:g[n])
        if(x!=p)
        {
            e[x]=e[n];
            e[x]+=c[n]-c[x];
            if(n>x) e[x]--;
            else e[x]++;
            reroot(g,c,e,x,n);
        }
    }
}
0