結果

問題 No.2427 Tree Distance Two
ユーザー InTheBloomInTheBloom
提出日時 2023-08-18 22:17:36
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 437 ms / 2,000 ms
コード長 783 bytes
コンパイル時間 3,771 ms
コンパイル使用メモリ 172,800 KB
実行使用メモリ 29,568 KB
最終ジャッジ日時 2024-11-28 07:54:35
合計ジャッジ時間 12,241 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N = readln.chomp.to!int;
    int[][] graph = new int[][](N, 0);
    foreach (i; 0..N-1) {
        int u, v; readln.read(u, v);
        u--, v--;
        graph[u] ~= v;
        graph[v] ~= u;
    }

    solve(N, graph);
}

void solve (int N, int[][] graph) {
    // スターグラフみたいなやつが来たら深さ2での探索うまくいかないんだよなぁってことで
    // 接続数を見ればいいんですね

    int[] ans = new int[](N);
    foreach (i; 0..N) {
        foreach (to; graph[i]) {
            ans[i] += graph[to].length-1;
        }
    }

    ans.each!writeln;
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0