結果

問題 No.828 全方位神童数
ユーザー koprickykopricky
提出日時 2019-04-12 00:39:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 5,092 bytes
コンパイル時間 1,663 ms
コンパイル使用メモリ 172,524 KB
実行使用メモリ 27,844 KB
最終ジャッジ日時 2024-06-10 06:10:42
合計ジャッジ時間 6,571 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,512 KB
testcase_01 AC 4 ms
8,904 KB
testcase_02 AC 4 ms
8,648 KB
testcase_03 AC 10 ms
9,668 KB
testcase_04 AC 6 ms
8,772 KB
testcase_05 AC 9 ms
9,280 KB
testcase_06 AC 5 ms
8,768 KB
testcase_07 AC 5 ms
8,772 KB
testcase_08 AC 5 ms
9,152 KB
testcase_09 AC 5 ms
9,028 KB
testcase_10 AC 7 ms
10,568 KB
testcase_11 AC 5 ms
8,900 KB
testcase_12 AC 8 ms
10,560 KB
testcase_13 AC 48 ms
14,404 KB
testcase_14 AC 113 ms
20,292 KB
testcase_15 AC 37 ms
13,508 KB
testcase_16 AC 51 ms
15,556 KB
testcase_17 AC 44 ms
13,380 KB
testcase_18 AC 76 ms
17,476 KB
testcase_19 AC 144 ms
22,980 KB
testcase_20 AC 90 ms
19,016 KB
testcase_21 AC 124 ms
21,188 KB
testcase_22 AC 27 ms
12,608 KB
testcase_23 AC 11 ms
10,056 KB
testcase_24 AC 75 ms
16,968 KB
testcase_25 AC 30 ms
11,844 KB
testcase_26 AC 145 ms
22,084 KB
testcase_27 AC 124 ms
20,268 KB
testcase_28 AC 150 ms
22,084 KB
testcase_29 AC 142 ms
21,316 KB
testcase_30 AC 72 ms
16,708 KB
testcase_31 AC 70 ms
15,812 KB
testcase_32 AC 147 ms
21,832 KB
testcase_33 AC 167 ms
23,104 KB
testcase_34 AC 124 ms
20,292 KB
testcase_35 AC 76 ms
16,964 KB
testcase_36 AC 97 ms
18,248 KB
testcase_37 AC 61 ms
15,048 KB
testcase_38 AC 88 ms
17,860 KB
testcase_39 AC 155 ms
21,956 KB
testcase_40 AC 63 ms
15,040 KB
testcase_41 AC 52 ms
15,040 KB
testcase_42 AC 165 ms
22,976 KB
testcase_43 AC 80 ms
27,844 KB
testcase_44 AC 33 ms
17,184 KB
testcase_45 AC 48 ms
19,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 200005;

template<typename T> class node {
public:
    int id;
    T val, lazy;
    node *left, *right, *par;
    node(int key, int value) : id(key), val(value), lazy(0),
        left(nullptr), right(nullptr), par(nullptr){}
    bool isRoot(){
        return !par;
    }
    void push(){
        if(lazy){
            val += lazy;
            if(left) left->lazy += lazy;
            if(right) right->lazy += lazy;
            lazy = 0;
        }
    }
    void rotate(bool right_){
        node<T> *p = par, *g = p->par;
        if(right_){
            if((p->left = right)) right->par = p;
            right = p, p->par = this;
        }else{
            if((p->right = left)) left->par = p;
            left = p, p->par = this;
        }
        if(!(par = g)) return;
        if(g->left == p) g->left = this;
        if(g->right == p) g->right = this;
    }
};

template<typename T>
node<T>* splay(node<T>* u){
    while(!(u->isRoot())){
        node<T> *p = u->par, *gp = p->par;
        if(p->isRoot()){ // zig
            p->push(), u->push();
            u->rotate((u == p->left));
        }else{
            gp->push(), p->push(), u->push();
            bool flag = (u == p->left);
            if((u == p->left) == (p == gp->left)){ // zig-zig
                p->rotate(flag), u->rotate(flag);
            }else{ // zig-zag
                u->rotate(flag), u->rotate(!flag);
            }
        }
    }
    u->push();
    return u;
}

// 存在しない key の場合何が返ってくるかは不定
template<typename T>
node<T>* find(const int key, node<T>* root)
{
    node<T> *cur = nullptr, *nx = root;
    while(nx){
        cur = nx, cur->push();
        if(cur->id == key) break;
        if(cur->id > key) nx = cur->left;
        else nx = cur->right;
    }
    return splay(cur);
}

template<typename T>
node<T>* join(node<T>* root1, node<T>* root2)
{
    if(!root1) return root2;
    node<T> *cur = nullptr, *nx = root1;
    while(nx){
        cur = nx, cur->push(), nx = cur->right;
    }
    node<T>* ver = splay(cur);
    ver->right = root2, root2->par = ver;
    return ver;
}

template<typename T>
node<T>* insert(node<T>* ver, node<T>* root)
{
    if(!root) return ver;
    node<T> *cur = nullptr, *nx = root;
    while(nx){
        cur = nx, cur->push();
        if(cur->id > ver->id) nx = cur->left;
        else nx = cur->right;
    }
    if(cur->id > ver->id) cur->left = ver, ver->par = cur;
    else cur->right = ver, ver->par = cur;
    return splay(ver);
}

template<typename T>
node<T>* erase(const int key, node<T>* root)
{
    node<T>* ver = find(key, root);
    return join(ver->left, ver->right);
}

template<typename T>
node<T>* merge(node<T>* root1, node<T>* root2)
{
    if(!root2) return root1;
    return insert(root2, root1);
}

vector<int> G[MAX_N];
int ans[MAX_N];
node<int>* ptr[MAX_N];

void dfs(node<int>* cur)
{
    cur->push();
    ans[cur->id] += cur->val;
    if(cur->left) dfs(cur->left);
    if(cur->right) dfs(cur->right);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        cin >> ans[i];
    }
    rep(i,n-1){
        int u, v;
        cin >> u >> v;
        if(u > v) swap(u, v);
        G[v-1].pb(u-1);
    }
    rep(i,n){
        ptr[i] = new node<int>(i, 1);
        each(v,G[i]){
            splay(ptr[v]), ptr[v]->lazy += 1;
			merge(ptr[v], ptr[i]);
        }
    }
    dfs(ptr[n-1]);
    int ret = 1;
    rep(i,n){
        ret = (ll)ret * ans[i] % MOD;
    }
    cout << ret << "\n";
    return 0;
}
0