結果

問題 No.828 全方位神童数
ユーザー rpy3cpp
提出日時 2019-05-08 23:37:50
言語 C++14
(gcc 8.3.0)
結果
AC  
実行時間 213 ms
コード長 1,717 Byte
コンパイル時間 1,525 ms
使用メモリ 29,284 KB
最終ジャッジ日時 2019-09-12 01:25:40

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
a_sample_1.txt AC 3 ms
1,532 KB
a_sample_2.txt AC 3 ms
1,536 KB
a_sample_3.txt AC 3 ms
1,552 KB
b_small_1.txt AC 10 ms
2,620 KB
b_small_2.txt AC 5 ms
1,824 KB
b_small_3.txt AC 9 ms
2,380 KB
b_small_4.txt AC 5 ms
1,796 KB
b_small_5.txt AC 4 ms
1,724 KB
b_small_6.txt AC 5 ms
1,792 KB
b_small_7.txt AC 4 ms
1,656 KB
b_small_8.txt AC 8 ms
2,156 KB
b_small_9.txt AC 4 ms
1,660 KB
b_small_10.txt AC 7 ms
2,356 KB
c_path_1.txt AC 58 ms
10,012 KB
c_path_2.txt AC 133 ms
19,248 KB
c_path_3.txt AC 39 ms
7,900 KB
c_path_4.txt AC 53 ms
10,536 KB
c_path_5.txt AC 43 ms
8,692 KB
c_path_6.txt AC 90 ms
14,232 KB
c_path_7.txt AC 169 ms
23,212 KB
c_path_8.txt AC 107 ms
16,344 KB
c_path_9.txt AC 140 ms
20,572 KB
c_path_10.txt AC 30 ms
6,052 KB
d_random_1.txt AC 12 ms
2,884 KB
d_random_2.txt AC 79 ms
12,912 KB
d_random_3.txt AC 32 ms
6,316 KB
d_random_4.txt AC 180 ms
21,888 KB
d_random_5.txt AC 149 ms
18,984 KB
d_random_6.txt AC 185 ms
22,156 KB
d_random_7.txt AC 166 ms
20,836 KB
d_random_8.txt AC 85 ms
12,916 KB
d_random_9.txt AC 78 ms
12,652 KB
d_random_10.txt AC 179 ms
21,888 KB
d_random_11.txt AC 195 ms
23,736 KB
d_random_12.txt AC 139 ms
19,244 KB
d_random_13.txt AC 86 ms
13,708 KB
d_random_14.txt AC 114 ms
16,348 KB
d_random_15.txt AC 76 ms
11,328 KB
d_random_16.txt AC 107 ms
15,024 KB
d_random_17.txt AC 181 ms
22,156 KB
d_random_18.txt AC 74 ms
11,332 KB
d_random_19.txt AC 59 ms
9,744 KB
d_random_20.txt AC 213 ms
24,004 KB
e_hand_1.txt AC 94 ms
29,284 KB
e_hand_2.txt AC 38 ms
12,136 KB
e_hand_3.txt AC 56 ms
17,664 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