結果

問題 No.828 全方位神童数
ユーザー kopricky
提出日時 2019-05-04 04:21:51
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 3,552 bytes
コンパイル時間 1,980 ms
コンパイル使用メモリ 165,916 KB
実行使用メモリ 28,024 KB
最終ジャッジ日時 2025-01-03 09:36:42
合計ジャッジ時間 10,028 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

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 segtree {
private:
    int n,sz;
    vector<T> node;
public:
    segtree(vector<T>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.assign(2*n,numeric_limits<T>::min());
        rep(i,sz){
            node[i+n] = v[i];
        }
        for(int i=n-1; i>=1; i--){
            node[i] = max(node[2*i],node[2*i+1]);
        }
    }
    void update(int k,T a)
    {
    	node[k+=n] = a;
    	while(k>>=1){
    		node[k] = max(node[2*k],node[2*k+1]);
    	}
    }
    T query(int a,int b)
    {
        T res1 = numeric_limits<T>::min();
        T res2 = numeric_limits<T>::min();
        a += n, b += n;
        while(a != b){
            if(a % 2) res1 = max(res1, node[a++]);
            if(b % 2) res2 = max(res2, node[--b]);
            a >>= 1, b>>= 1;
        }
        return max(res1, res2);
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};

vector<int> G[MAX_N], ord;
int r[MAX_N], in[MAX_N], out[MAX_N], ans[MAX_N];

void dfs(int u, int p)
{
    in[u] = len(ord);
    ord.pb(u);
    each(v, G[u]){
        if(v != p) dfs(v, u);
    }
    out[u] = len(ord);
}

void rec(int u, int p, int val, segtree<int>& sg){
    for(;;val++){
        int ver = sg.query(in[u], out[u]);
        if(ver == -INF) break;
        ans[ver] = val + 1;
        each(nx, G[ver]){
            if(in[nx] > in[ver]) rec(nx, ver, val + 1, sg);
        }
        sg.update(in[ver], -INF);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        cin >> r[i];
    }
    rep(i,n-1){
        int u, v;
        cin >> u >> v;
        G[u-1].pb(v-1), G[v-1].pb(u-1);
    }
    dfs(0, -1);
    segtree<int> sg(ord);
    rec(0, -1, 0, sg);
    int score = 1;
    rep(i,n){
        score = (ll)score * (r[i] + ans[i]) % MOD;
    }
    cout << score << "\n";
    return 0;
}
0