結果

問題 No.1817 Reversed Edges
ユーザー ripityripity
提出日時 2022-01-21 21:57:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 2,161 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 76,308 KB
実行使用メモリ 82,008 KB
最終ジャッジ日時 2023-08-17 05:53:50
合計ジャッジ時間 25,031 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,748 KB
testcase_01 AC 125 ms
56,120 KB
testcase_02 AC 125 ms
56,124 KB
testcase_03 AC 125 ms
56,356 KB
testcase_04 AC 123 ms
55,772 KB
testcase_05 AC 124 ms
56,360 KB
testcase_06 AC 123 ms
56,008 KB
testcase_07 AC 1,105 ms
79,320 KB
testcase_08 AC 756 ms
69,156 KB
testcase_09 AC 1,106 ms
79,156 KB
testcase_10 AC 790 ms
71,512 KB
testcase_11 AC 941 ms
71,964 KB
testcase_12 AC 1,247 ms
81,224 KB
testcase_13 AC 1,253 ms
80,812 KB
testcase_14 AC 1,212 ms
81,192 KB
testcase_15 AC 1,256 ms
81,492 KB
testcase_16 AC 1,212 ms
81,308 KB
testcase_17 AC 1,205 ms
80,612 KB
testcase_18 AC 1,203 ms
81,388 KB
testcase_19 AC 1,249 ms
80,716 KB
testcase_20 AC 1,211 ms
81,376 KB
testcase_21 AC 1,215 ms
81,376 KB
testcase_22 AC 931 ms
81,464 KB
testcase_23 AC 934 ms
81,800 KB
testcase_24 AC 963 ms
82,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    
    public static Scanner sc = new Scanner(System.in);
    public static PrintWriter pw = new PrintWriter(System.out);
    
    public static void main(String[] args) {
        
        int T = 1;
        while( T > 0 ) {
            solve();
            T--;
        }
        
        pw.flush();
        
    }
    
    static void solve() {
        
        int N = sc.nextInt();
        int[] dx = new int[N];
        int[] dy = new int[N];
        boolean[] vx = new boolean[N];
        boolean[] vy = new boolean[N];
        int offset = 0;
        LinkedList<Integer> deque = new LinkedList<>();
        ArrayList<ArrayList<Integer>> edge = new ArrayList<>();
        for( int i = 0; i < N; i++ ) {
            edge.add(new ArrayList<>());
            dx[i] = dy[i] = Integer.MAX_VALUE;
        }
        
        for( int i = 0; i < N-1; i++ ) {
            int a = sc.nextInt()-1;
            int b = sc.nextInt()-1;
            edge.get(a).add(b);
            edge.get(b).add(a);
        }
        
        dx[0] = dy[0] = 0;
        deque.offer(0);
        while( !deque.isEmpty() ) {
            int now = deque.poll();
            if( vx[now] ) continue;
            vx[now] = true;
            for( int next : edge.get(now) ) {
                if( !vx[next] && dx[now]+1 < dx[next] ) {
                    dx[next] = dx[now]+1;
                    deque.offer(next);
                }
            }
        }
        
        deque.offer(0);
        while( !deque.isEmpty() ) {
            int now = deque.poll();
            if( vy[now] ) continue;
            vy[now] = true;
            for( int next : edge.get(now) ) {
                int k = now > next ? 1 : 0;
                if( !vy[next] && dy[now]+k < dy[next] ) {
                    offset += k;
                    dy[next] = dy[now]+k;
                    deque.offer(next);
                }
            }
        }
        
        // pw.println(offset);
        for( int i = 0; i < N; i++ ) {
            // pw.println(dx[i]+" "+dy[i]);
            pw.println(offset+dx[i]-2*dy[i]);
        }
        
    }
    
}
0