結果

問題 No.828 全方位神童数
ユーザー koprickykopricky
提出日時 2019-05-04 04:21:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 3,552 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 170,092 KB
実行使用メモリ 28,272 KB
最終ジャッジ日時 2023-09-01 22:28:03
合計ジャッジ時間 10,910 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,184 KB
testcase_01 AC 4 ms
9,180 KB
testcase_02 AC 4 ms
9,360 KB
testcase_03 AC 10 ms
9,732 KB
testcase_04 AC 6 ms
9,452 KB
testcase_05 AC 10 ms
9,660 KB
testcase_06 AC 5 ms
9,300 KB
testcase_07 AC 5 ms
9,392 KB
testcase_08 AC 6 ms
9,332 KB
testcase_09 AC 4 ms
9,484 KB
testcase_10 AC 9 ms
9,612 KB
testcase_11 AC 5 ms
9,220 KB
testcase_12 AC 9 ms
9,872 KB
testcase_13 AC 60 ms
16,356 KB
testcase_14 AC 148 ms
24,084 KB
testcase_15 AC 43 ms
15,240 KB
testcase_16 AC 64 ms
16,952 KB
testcase_17 AC 51 ms
15,004 KB
testcase_18 AC 95 ms
18,892 KB
testcase_19 AC 185 ms
28,272 KB
testcase_20 AC 112 ms
22,588 KB
testcase_21 AC 156 ms
25,924 KB
testcase_22 AC 30 ms
13,452 KB
testcase_23 AC 12 ms
9,984 KB
testcase_24 AC 85 ms
14,480 KB
testcase_25 AC 31 ms
11,312 KB
testcase_26 AC 185 ms
18,940 KB
testcase_27 AC 155 ms
18,108 KB
testcase_28 AC 177 ms
19,284 KB
testcase_29 AC 161 ms
18,584 KB
testcase_30 AC 83 ms
14,464 KB
testcase_31 AC 81 ms
14,364 KB
testcase_32 AC 192 ms
18,940 KB
testcase_33 AC 213 ms
19,680 KB
testcase_34 AC 164 ms
18,112 KB
testcase_35 AC 90 ms
14,728 KB
testcase_36 AC 135 ms
16,096 KB
testcase_37 AC 75 ms
14,096 KB
testcase_38 AC 112 ms
15,544 KB
testcase_39 AC 197 ms
19,048 KB
testcase_40 AC 77 ms
13,956 KB
testcase_41 AC 60 ms
13,476 KB
testcase_42 AC 222 ms
19,768 KB
testcase_43 AC 101 ms
28,192 KB
testcase_44 AC 40 ms
16,200 KB
testcase_45 AC 60 ms
20,324 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 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