結果

問題 No.828 全方位神童数
ユーザー pekempeypekempey
提出日時 2019-05-03 23:55:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,358 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 74,240 KB
実行使用メモリ 18,480 KB
最終ジャッジ日時 2023-08-30 07:03:54
合計ジャッジ時間 8,040 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,740 KB
testcase_01 AC 4 ms
11,032 KB
testcase_02 AC 4 ms
10,740 KB
testcase_03 AC 15 ms
11,156 KB
testcase_04 AC 7 ms
11,024 KB
testcase_05 AC 14 ms
11,104 KB
testcase_06 AC 6 ms
10,816 KB
testcase_07 AC 5 ms
10,768 KB
testcase_08 AC 6 ms
10,844 KB
testcase_09 AC 5 ms
11,024 KB
testcase_10 AC 11 ms
11,012 KB
testcase_11 AC 5 ms
10,744 KB
testcase_12 AC 12 ms
11,028 KB
testcase_13 AC 85 ms
13,744 KB
testcase_14 AC 181 ms
16,872 KB
testcase_15 AC 65 ms
13,040 KB
testcase_16 AC 90 ms
13,916 KB
testcase_17 AC 73 ms
13,512 KB
testcase_18 AC 125 ms
14,984 KB
testcase_19 AC 223 ms
18,028 KB
testcase_20 AC 150 ms
15,776 KB
testcase_21 AC 195 ms
17,136 KB
testcase_22 AC 46 ms
12,428 KB
testcase_23 AC 17 ms
11,428 KB
testcase_24 AC 125 ms
14,676 KB
testcase_25 AC 50 ms
12,480 KB
testcase_26 AC 235 ms
17,620 KB
testcase_27 AC 201 ms
16,632 KB
testcase_28 AC 237 ms
17,640 KB
testcase_29 AC 221 ms
17,284 KB
testcase_30 AC 121 ms
14,652 KB
testcase_31 AC 117 ms
14,556 KB
testcase_32 AC 235 ms
17,640 KB
testcase_33 AC 258 ms
18,220 KB
testcase_34 AC 201 ms
16,688 KB
testcase_35 AC 134 ms
14,932 KB
testcase_36 AC 167 ms
15,772 KB
testcase_37 AC 105 ms
14,412 KB
testcase_38 AC 148 ms
15,304 KB
testcase_39 AC 243 ms
17,680 KB
testcase_40 AC 104 ms
14,196 KB
testcase_41 AC 86 ms
13,732 KB
testcase_42 AC 262 ms
18,480 KB
testcase_43 AC 165 ms
16,908 KB
testcase_44 AC 66 ms
13,544 KB
testcase_45 AC 97 ms
14,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int MOD = 1e9 + 7;

struct mint {
    int n;
    mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b) { a.n += b.n; if (a.n >= MOD) a.n -= MOD; return a; }
mint operator-(mint a, mint b) { a.n -= b.n; if (a.n < 0) a.n += MOD; return a; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

int N, X[200000];
vector<int> G[200000];

int P[400000];
int L[400000];
int R[400000];
int K;
mint ans = 1;

int find(int x) {
  if (P[x] == x) return x;
  return P[x] = find(P[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  L[K] = x;
  R[K] = y;
  P[x] = K;
  P[y] = K;
  K++;
}

void dfs(int u, int d) {
  if (u < N) {
    ans *= X[u] + d;
    return;
  }
  dfs(L[u], d);
  dfs(R[u], d+1);
}

int main() {
  cin >> N;
  REP(i, N) cin >> X[i];
  REP(i, N*2) {
    P[i] = i;
  }
  K = N;
  REP(i, N-1) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  REP(i, N) for (int j : G[i]) if (i > j) unite(i, j);
  dfs(K-1, 1);
  cout << ans.n << '\n';
}
0