結果

問題 No.828 全方位神童数
ユーザー polyomino_24polyomino_24
提出日時 2019-05-03 23:16:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 6,532 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 138,736 KB
実行使用メモリ 34,880 KB
最終ジャッジ日時 2024-06-10 07:08:42
合計ジャッジ時間 8,299 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,192 KB
testcase_01 AC 4 ms
8,064 KB
testcase_02 AC 4 ms
8,192 KB
testcase_03 AC 14 ms
9,256 KB
testcase_04 AC 7 ms
8,320 KB
testcase_05 AC 14 ms
9,240 KB
testcase_06 AC 6 ms
8,448 KB
testcase_07 AC 6 ms
8,448 KB
testcase_08 AC 6 ms
8,524 KB
testcase_09 AC 5 ms
8,320 KB
testcase_10 AC 11 ms
8,960 KB
testcase_11 AC 5 ms
8,320 KB
testcase_12 AC 12 ms
8,940 KB
testcase_13 AC 97 ms
18,332 KB
testcase_14 AC 204 ms
30,252 KB
testcase_15 AC 66 ms
15,628 KB
testcase_16 AC 97 ms
18,808 KB
testcase_17 AC 76 ms
16,600 KB
testcase_18 AC 137 ms
23,692 KB
testcase_19 AC 251 ms
34,880 KB
testcase_20 AC 165 ms
26,588 KB
testcase_21 AC 210 ms
31,928 KB
testcase_22 AC 54 ms
13,512 KB
testcase_23 AC 19 ms
9,472 KB
testcase_24 AC 129 ms
20,216 KB
testcase_25 AC 58 ms
13,080 KB
testcase_26 AC 241 ms
30,288 KB
testcase_27 AC 208 ms
27,768 KB
testcase_28 AC 232 ms
30,516 KB
testcase_29 AC 215 ms
29,528 KB
testcase_30 AC 119 ms
20,556 KB
testcase_31 AC 115 ms
20,204 KB
testcase_32 AC 254 ms
30,656 KB
testcase_33 AC 273 ms
32,372 KB
testcase_34 AC 209 ms
26,968 KB
testcase_35 AC 132 ms
20,904 KB
testcase_36 AC 162 ms
23,932 KB
testcase_37 AC 106 ms
18,560 KB
testcase_38 AC 143 ms
22,400 KB
testcase_39 AC 238 ms
30,128 KB
testcase_40 AC 104 ms
19,028 KB
testcase_41 AC 85 ms
17,120 KB
testcase_42 AC 244 ms
31,936 KB
testcase_43 AC 170 ms
27,188 KB
testcase_44 AC 68 ms
15,424 KB
testcase_45 AC 101 ms
19,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}

template<int MOD>
class modint{
public:
    int a;
    modint(const long long v = 0):a(v % MOD){}
    int getmod() const{
        return MOD;
    }
    modint operator+(const modint rhs) const{
        return modint(*this) += rhs;
    }
    modint operator-(const modint rhs) const{
        return modint(*this) -= rhs;
    }
    modint operator*(const modint rhs) const{
        return modint(*this) *= rhs;
    }
    modint operator/(const modint rhs) const{
        return modint(*this) /= rhs;
    }
    modint operator+(const long long rhs) const{
        return modint(*this) += rhs;
    }
    modint operator-(const long long rhs) const{
        return modint(*this) -= rhs;
    }
    modint operator*(const long long rhs) const{
        return modint(*this) *= rhs;
    }
    modint operator/(const long long rhs) const{
        return modint(*this) /= rhs;
    }
    friend modint operator+(const long long a, const modint b){
        return b + a;
    }
    friend modint operator-(const long long a, const modint b){
        return -b + a;
    }
    friend modint operator*(const long long a, const modint b){
        return b * a;
    }
    friend modint operator/(const long long a, const modint b){
        return modint(a) / b;
    }
    modint &operator+=(const modint rhs){
        a += rhs.a;
        if(a >= MOD){
            a -= MOD;
        }
        return *this;
    }
    modint &operator-=(const modint rhs){
        if(a < rhs.a){
            a += MOD;
        }
        a -= rhs.a;
        return *this;
    }
    modint &operator*=(const modint rhs){
        a = (long long)a * rhs.a % MOD;
        return *this;
    }
    modint &operator/=(modint rhs){
        int x = MOD - 2;
        while(x){
            if(x % 2){
                *this *= rhs;
            }
            rhs *= rhs;
            x /= 2;
        }
        return *this;
    }
    //https://mickey24.hatenablog.com/entry/20081021/1224590753
    modint &operator++(){
        *this += 1;
        return *this;
    }
    modint &operator--(){
        *this -= 1;
        return *this;
    }
    modint operator++(int){
        modint res = *this;
        ++(*this);
        return res;
    }
    modint operator--(int){
        modint res = *this;
        res -= 1;
        return res;
    }
    modint &operator+=(const long long rhs){
        *this += modint(rhs);
        return *this;
    }
    modint &operator-=(const long long rhs){
        *this -= modint(rhs);
        return *this;
    }
    modint &operator*=(const long long rhs){
        *this *= modint(rhs);
        return *this;
    }
    modint &operator/=(const long long rhs){
        *this /= modint(rhs);
        return *this;
    }
    modint operator+() const{
        return *this;
    }
    modint operator-() const{
        return modint()-*this;
    }
    bool operator==(const modint rhs) const{
        return a == rhs.a;
    }
    bool operator==(const long long rhs) const{
        return a == rhs;
    }
    friend bool operator==(const long long a, const modint b){
        return a == b.a;
    }
    bool operator!=(const modint rhs) const{
        return a != rhs.a;
    }
    bool operator!=(const long long rhs) const{
        return a != rhs;
    }
    friend ostream &operator<<(ostream &os, const modint x){
        os << x.a;
        return os;
    }
    friend istream &operator>>(istream &is, modint &x){
        is >> x.a;
        return is;
    }
    explicit operator bool() const{
        return a > 0;
    }
    bool operator!(){
        return a == 0;
    }
    explicit operator int() const{
        return a;
    }
    explicit operator long long() const{
        return (long long) a;
    }
    friend modint pow(modint a, long long b){
        modint res = 1;
        while(b){
            if(b % 2){
                res *= a;
            }
            a *= a;
            b /= 2;
        }
        return res;
    }
};
using mint = modint<1000000007>;
vector<int>g[200005];
int par[200005];
int sz[200005];
vector<vector<int>>d;
int find(int x){
    if(par[x] == x)return x;
    else return par[x] = find(par[x]);
}
bool same(int x,int y){
    x = find(x);
    y = find(y);
    return x == y;
}
void unite(int x,int y){
    x = find(x);
    y = find(y);
    if(x == y)return;
    if(sz[x] >= sz[y]){
        for(auto z:d[y]){
            d[x].push_back(z);
        }
        sz[x] += sz[y];
        par[y] = x;
        sz[y] = 0;
    }else{
        swap(x,y);
        for(auto z:d[y]){
            d[x].push_back(z);
        }
        sz[x] += sz[y];
        par[y] = x;
        sz[y] = 0;
    }
}
int main(){
    int n;
    cin >> n;
    vector<mint>r(n);
    rep(i,n)cin >> r[i];
    d.resize(n);
    rep(i,n){
        sz[i] = 1;
        par[i] = i;
        d[i].push_back(i);
    }
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        a--,b--;
        if(b > a)swap(a,b);
        g[a].push_back(b);
    }
    vector<pii>p;
    rep(i,n){
        for(auto x: g[i]){
            unite(i,x);
        }
        int t = find(i);
        show(i,t);
        p.emplace_back(d[t][0],d[t].back());
    }
    auto a = d[find(0)];
    vector<int>gya(n);
    vector<int>b(n+1,0);
    rep(i,n){
        gya[a[i]] = i;
    }
    for(auto x : p){
        b[gya[x.first]]++;
        b[gya[x.second]+1]--;
    }
    rep(i,n)b[i+1] += b[i];
    mint ans = 1;
    rep(i,n){
        ans *= (r[i] + b[gya[i]]);
    }
    cout << ans << endl;
}
0