結果
問題 | No.828 全方位神童数 |
ユーザー | kopricky |
提出日時 | 2019-05-06 10:16:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,088 ms / 2,000 ms |
コード長 | 10,646 bytes |
コンパイル時間 | 2,092 ms |
コンパイル使用メモリ | 182,148 KB |
実行使用メモリ | 91,904 KB |
最終ジャッジ日時 | 2024-06-27 04:41:48 |
合計ジャッジ時間 | 23,678 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 27 ms
7,032 KB |
testcase_04 | AC | 8 ms
6,940 KB |
testcase_05 | AC | 24 ms
6,940 KB |
testcase_06 | AC | 6 ms
6,944 KB |
testcase_07 | AC | 5 ms
6,944 KB |
testcase_08 | AC | 6 ms
6,940 KB |
testcase_09 | AC | 4 ms
6,940 KB |
testcase_10 | AC | 18 ms
6,940 KB |
testcase_11 | AC | 4 ms
6,940 KB |
testcase_12 | AC | 19 ms
6,940 KB |
testcase_13 | AC | 303 ms
37,248 KB |
testcase_14 | AC | 796 ms
73,736 KB |
testcase_15 | AC | 209 ms
30,464 KB |
testcase_16 | AC | 357 ms
39,552 KB |
testcase_17 | AC | 250 ms
32,000 KB |
testcase_18 | AC | 524 ms
52,096 KB |
testcase_19 | AC | 1,007 ms
91,904 KB |
testcase_20 | AC | 656 ms
65,680 KB |
testcase_21 | AC | 834 ms
80,524 KB |
testcase_22 | AC | 124 ms
22,528 KB |
testcase_23 | AC | 32 ms
7,936 KB |
testcase_24 | AC | 444 ms
40,960 KB |
testcase_25 | AC | 139 ms
18,944 KB |
testcase_26 | AC | 934 ms
70,452 KB |
testcase_27 | AC | 768 ms
60,848 KB |
testcase_28 | AC | 921 ms
70,952 KB |
testcase_29 | AC | 847 ms
67,196 KB |
testcase_30 | AC | 444 ms
40,704 KB |
testcase_31 | AC | 410 ms
39,552 KB |
testcase_32 | AC | 916 ms
70,368 KB |
testcase_33 | AC | 1,016 ms
76,292 KB |
testcase_34 | AC | 807 ms
61,568 KB |
testcase_35 | AC | 507 ms
43,008 KB |
testcase_36 | AC | 646 ms
51,968 KB |
testcase_37 | AC | 375 ms
35,840 KB |
testcase_38 | AC | 562 ms
47,488 KB |
testcase_39 | AC | 954 ms
70,868 KB |
testcase_40 | AC | 381 ms
35,712 KB |
testcase_41 | AC | 308 ms
30,336 KB |
testcase_42 | AC | 1,088 ms
76,756 KB |
testcase_43 | AC | 335 ms
84,216 KB |
testcase_44 | AC | 123 ms
34,816 KB |
testcase_45 | AC | 188 ms
50,816 KB |
ソースコード
#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 _Tp> class BSTNode { public: int from, to, sz; _Tp value, al, lazy; BSTNode *left, *right, *par; BSTNode(int _from, int _to) noexcept : from(_from), to(_to), sz(1), value(id1), al(id2), lazy(id1), left(nullptr), right(nullptr), par(nullptr){} static const _Tp id1 = (_Tp)0; static const _Tp id2 = (_Tp)0; static void opr1(_Tp& arg1, const _Tp arg2){ arg1 += arg2; } static _Tp opr2(const _Tp arg1, const _Tp arg2){ return arg1 + arg2; } inline bool isRoot() const { return !par; } inline bool isVertex() const noexcept { return from == to; } void push(){ if(lazy != id1){ opr1(value, lazy), al += lazy * sz; if(left) opr1(left->lazy, lazy); if(right) opr1(right->lazy, lazy); lazy = id1; } } void eval(){ sz = 1, al = value; if(left) left->push(), sz += left->sz, al = opr2(left->al, al); if(right) right->push(), sz += right->sz, al = opr2(al, right->al); } void rotate(bool right_){ BSTNode *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; } p->eval(), eval(); if(!(par = g)) return; if(g->left == p) g->left = this; if(g->right == p) g->right = this; g->eval(); } }; template<typename _Tp> BSTNode<_Tp>* splay(BSTNode<_Tp> *u) noexcept { if(!u) return nullptr; while(!(u->isRoot())){ BSTNode<_Tp> *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; } template<typename _Tp> BSTNode<_Tp>* join(BSTNode<_Tp> *root1, BSTNode<_Tp> *root2) noexcept { if(!root1 || !root2) return root1 ? root1 : root2; BSTNode<_Tp> *cur = nullptr, *nx = root1; do{ cur = nx, cur->push(), nx = cur->right; }while(nx); BSTNode<_Tp> *ver = splay(cur); ver->right = root2, ver->eval(), root2->par = ver; return ver; } template<typename _Tp> pair<BSTNode<_Tp>*, BSTNode<_Tp>*> split_lower_bond(BSTNode<_Tp> *ver) noexcept { BSTNode<_Tp> *res = splay(ver)->left; ver->left = nullptr, ver->eval(); if(res) res->par = nullptr; return make_pair(res, ver); } template<typename _Tp> pair<BSTNode<_Tp>*, BSTNode<_Tp>*> split_upper_bond(BSTNode<_Tp> *ver) noexcept { BSTNode<_Tp> *res = splay(ver)->right; ver->right = nullptr, ver->eval(); if(res) res->par = nullptr; return make_pair(ver, res); } template<typename T> class EulerTourTree { private: struct edge { int from, to; edge(){} edge(int _from, int _to) : from(_from), to(_to){} bool operator<(const edge& val) const { return (from == val.from) ? (to < val.to) : (from < val.from); } }; void reroot(BSTNode<T> *ver){ BSTNode<T> *res = splay(ver)->left; if(!res) return; while(ver->right) ver->push(), ver = ver->right; ver->right = res, ver->eval(), res->par = ver, splay(res); } void link(BSTNode<T> *ver1, BSTNode<T> *ver2){ reroot(ver1), reroot(ver2); BSTNode<T>* e1 = new BSTNode<T>(ver1->from, ver2->from); BSTNode<T>* e2 = new BSTNode<T>(ver2->from, ver1->from); edge_set[(edge){ver1->from, ver2->from}] = e1; edge_set[(edge){ver2->from, ver1->from}] = e2; join(join(ver1, e1), join(ver2, e2)); } void cut(BSTNode<T> *edge1, BSTNode<T> *edge2){ splay(edge1), splay(edge2); BSTNode<T> *p = edge1->par; if(p != edge2){ p->par = nullptr; if(edge1 == p->left) edge1->rotate(true), edge2->left = edge1; else edge1->rotate(false), edge2->right = edge1; } if(edge1->left) edge1->left->par = nullptr; if(edge1->right) edge1->right->par = nullptr; if(edge1 == edge2->left){ if(edge2->right) edge2->right->par = nullptr; join(edge1->left, edge2->right); }else{ if(edge2->left) edge2->left->par = nullptr; join(edge2->left, edge1->right); } delete edge1; delete edge2; } // void cut(BSTNode<T> *edge1, BSTNode<T> *edge2){ // splay(edge1), splay(edge2); // BSTNode<T> *p = edge1->par; // if(p != edge2) p->par = nullptr, edge1->rotate((edge1 == p->left)); // if(edge2->left) edge2->left->par = nullptr; // if(edge1->left) edge1->left->par = nullptr; // if(edge1->right) edge1->right->par = nullptr; // delete edge1; delete edge2; // } bool isConnected(BSTNode<T> *ver1, BSTNode<T> *ver2){ splay(ver1), splay(ver2); return ver1->par; } T& get(BSTNode<T> *ver){ return splay(ver)->value; } void range(BSTNode<T> *edge1, BSTNode<T> *edge2, const T& val){ auto res1 = split_lower_bond(edge1); auto res2 = split_upper_bond(edge2); BSTNode<T>::opr1(res2.first->lazy, val); join(join(res1.first,res2.first), res2.second); } T query(BSTNode<T> *edge1, BSTNode<T> *edge2){ auto res1 = split_lower_bond(edge1); auto res2 = split_upper_bond(edge2); T res = res2.first->al; return join(join(res1.first,res2.first), res2.second), res; } public: int V; BSTNode<T>** vertex_set; map<edge, BSTNode<T>*> edge_set; EulerTourTree(const int node_size) noexcept : V(node_size){ vertex_set = new BSTNode<T>*[V]; for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode<T>(i, i); } void reroot(const int node_id) noexcept { reroot(vertex_set[node_id]); } void link(const int node1_id, const int node2_id) noexcept { link(vertex_set[node1_id], vertex_set[node2_id]); } void cut(const int node1_id, const int node2_id){ auto it1 = edge_set.find((edge){node1_id, node2_id}); auto it2 = edge_set.find((edge){node2_id, node1_id}); assert(it1 != edge_set.end()); cut(it1->se, it2->se); } bool isConnected(const int node1_id, const int node2_id) noexcept { return isConnected(vertex_set[node1_id], vertex_set[node2_id]);} T& get(int ver_id){ return get(vertex_set[ver_id]); } void component_range(int ver_id, const T& val) noexcept { range(ver_id, -1, val); } void range(int ver_id, int par_id, const T& val) noexcept { if(par_id < 0) return BSTNode<T>::opr1(splay(vertex_set[ver_id])->lazy, val); auto it1 = edge_set.find((edge){par_id, ver_id}); auto it2 = edge_set.find((edge){ver_id, par_id}); assert(it1 != edge_set.end()); range(it1->se, it2->se, val); } T component_query(int ver_id) noexcept { return query(ver_id, -1); } T query(int ver_id, int par_id) noexcept { if(par_id < 0) return splay(vertex_set[ver_id])->al; auto it1 = edge_set.find((edge){par_id, ver_id}); auto it2 = edge_set.find((edge){ver_id, par_id}); assert(it1 != edge_set.end()); return query(it1->se, it2->se); } vector<vector<int> > G; vector<int> parent; EulerTourTree(const int node_size, bool _build) noexcept : V(node_size), G(V), parent(V){ vertex_set = new BSTNode<T>*[V]; for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode<T>(i, i); } void add_edge(int u, int v){ G[u].push_back(v), G[v].push_back(u); } BSTNode<T>* dfs(int u, int p, BSTNode<T> *cur){ parent[u] = p; cur = join(cur, vertex_set[u]); for(auto& v : G[u]){ if(v != p){ BSTNode<T>* e1 = new BSTNode<T>(u, v); edge_set[(edge){u, v}] = e1; cur = join(cur, e1); cur = dfs(v, u, cur); BSTNode<T>* e2 = new BSTNode<T>(v, u); edge_set[(edge){v, u}] = e2; cur = join(cur, e2); } } return cur; } vector<int> build(int _root){ BSTNode<T> *cur = nullptr; dfs(_root, -1, cur); return parent; } }; int r[MAX_N]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; rep(i,n){ cin >> r[i]; } EulerTourTree<int> et(n, true); rep(i,n-1){ int u, v; cin >> u >> v; et.add_edge(u-1, v-1); } auto par = et.build(0); rrep(i,n){ et.component_range(i, 1); for(auto& to : et.G[i]) if(to < i) et.cut(to, i); } int ans = 1; rep(i,n){ ans = (ll)ans * (et.get(i) + r[i]) % MOD; } cout << ans << "\n"; return 0; }