結果

問題 No.399 動的な領主
ユーザー nebukuro09nebukuro09
提出日時 2017-06-02 17:05:55
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 105,628 KB
実行使用メモリ 33,832 KB
最終ジャッジ日時 2023-09-03 13:54:48
合計ジャッジ時間 6,244 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,508 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 26 ms
4,624 KB
testcase_06 AC 338 ms
22,764 KB
testcase_07 AC 365 ms
22,184 KB
testcase_08 AC 351 ms
21,852 KB
testcase_09 AC 368 ms
22,192 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 26 ms
4,680 KB
testcase_12 AC 327 ms
21,844 KB
testcase_13 AC 318 ms
22,496 KB
testcase_14 AC 209 ms
33,320 KB
testcase_15 AC 220 ms
33,832 KB
testcase_16 AC 221 ms
28,644 KB
testcase_17 AC 363 ms
21,640 KB
testcase_18 AC 349 ms
21,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto edges = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        edges[s[0] - 1] ~= s[1] - 1;
        edges[s[1] - 1] ~= s[0] - 1;
    }


    auto depth = new int[](N);
    auto dp = new int[][](20, N);
    
    void dfs(int n, int p, int d) {
        dp[0][n] = p;
        depth[n] = d;
        foreach (m; edges[n]) if (m != p) dfs(m, n, d+1);
    }
    
    dfs(0, -1, 0);
 
    foreach (k; 0..19)
        foreach (n; 0..N)
            dp[k+1][n] = (dp[k][n] == -1) ? -1 : dp[k][dp[k][n]];
 
    int lca(int a, int b) {
        if (depth[a] < depth[b]) swap(a, b);
        
        auto orig_a = a;
        auto orig_b = b;
        
        while (depth[a] > depth[b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if (2^^k <= depth[a] - depth[b]) {
                    a = dp[k][a];
                    K = k;
                }
            }
        }
        
        if (a == b) return a;
        
        while (dp[0][a] != dp[0][b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if (dp[k][a] != dp[k][b]) {
                    a = dp[k][a];
                    b = dp[k][b];
                    K = k;
                }
            }
        }
        
        return dp[0][a];
    }
  

    auto imos = new long[](N);
    fill(imos, 0);
    
    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto s = readln.split.map!(to!int);
        auto a = s[0] - 1;
        auto b = s[1] - 1;
        auto c = lca(a, b);
        imos[a] += 1;
        imos[b] += 1;
        imos[c] -= 1;
        if (c > 0) imos[dp[0][c]] -= 1;
        //else imos[0] -= 1;
    }


    long dfs2(int n, int prev) {
        foreach (m; edges[n]) {
            if (m != prev) imos[n] += dfs2(m, n);
        }
        return imos[n];
    }

    dfs2(0, -1);
    imos.map!(a => a * (a + 1) / 2).sum.writeln;
}
0