結果

問題 No.828 全方位神童数
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-08 23:37:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 1,717 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 175,292 KB
実行使用メモリ 33,400 KB
最終ジャッジ日時 2023-09-14 17:26:34
合計ジャッジ時間 10,116 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 8 ms
4,392 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 56 ms
11,760 KB
testcase_14 AC 129 ms
21,000 KB
testcase_15 AC 41 ms
9,500 KB
testcase_16 AC 59 ms
11,988 KB
testcase_17 AC 46 ms
10,312 KB
testcase_18 AC 87 ms
15,732 KB
testcase_19 AC 162 ms
24,992 KB
testcase_20 AC 107 ms
18,020 KB
testcase_21 AC 142 ms
22,248 KB
testcase_22 AC 29 ms
7,816 KB
testcase_23 AC 10 ms
4,612 KB
testcase_24 AC 94 ms
14,640 KB
testcase_25 AC 33 ms
7,836 KB
testcase_26 AC 181 ms
23,728 KB
testcase_27 AC 154 ms
20,588 KB
testcase_28 AC 183 ms
23,756 KB
testcase_29 AC 172 ms
22,648 KB
testcase_30 AC 88 ms
14,608 KB
testcase_31 AC 91 ms
14,316 KB
testcase_32 AC 184 ms
23,424 KB
testcase_33 AC 209 ms
25,348 KB
testcase_34 AC 151 ms
20,844 KB
testcase_35 AC 99 ms
15,176 KB
testcase_36 AC 119 ms
18,224 KB
testcase_37 AC 75 ms
13,092 KB
testcase_38 AC 111 ms
16,644 KB
testcase_39 AC 183 ms
23,776 KB
testcase_40 AC 76 ms
13,016 KB
testcase_41 AC 61 ms
11,420 KB
testcase_42 AC 195 ms
25,876 KB
testcase_43 AC 94 ms
33,400 KB
testcase_44 AC 37 ms
14,864 KB
testcase_45 AC 56 ms
20,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1e9+7;

struct UnionFind{
    vector<int> data, max;
    UnionFind(int size) : data(size, -1), max(size, 0){
        for (int i = 0; i < size; ++i) max[i] = i;
    }
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if (x != y){
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        if (max[x] > max[y]){
            max[y] = max[x];
        }else{
            max[x] = max[y];
        }
    }
    int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);}
    int size(int x) {return -data[root(x)];}
    int head(int x) {return max[root(x)];}
};

void dfs(int v, vector<int> & depth, const vector<vector<int>> &Fs){
    for (auto u: Fs[v]){
        depth[u] = depth[v] + 1;
        dfs(u, depth, Fs);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<long long> rs(N, 0LL);
    for (auto & r: rs) cin >> r;
    vector<vector<int>> Es(N, vector<int>());
    for (int i = 1; i < N; ++i){
        int a, b;
        cin >> a >> b;
        Es[a-1].push_back(b-1);
        Es[b-1].push_back(a-1);
    }
    vector<vector<int>> Fs(N, vector<int>());
    UnionFind uf(N);
    for (int v = 0; v < N; ++v){
        for (auto u: Es[v]){
            if (u < v){
                Fs[v].push_back(uf.head(u));
                uf.unite(u, v);
            }
        }
    }
    vector<int> depth(N, 0);
    dfs(N-1, depth, Fs);
    long long ans = 1LL;
    for (int i = 0; i < N; ++i){
        ans *= (1 + depth[i] + rs[i]) % mod;
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}
0