結果
問題 | No.828 全方位神童数 |
ユーザー | kopricky |
提出日時 | 2019-10-19 17:46:36 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 593 ms / 2,000 ms |
コード長 | 14,749 bytes |
コンパイル時間 | 1,991 ms |
コンパイル使用メモリ | 187,748 KB |
実行使用メモリ | 84,236 KB |
最終ジャッジ日時 | 2024-06-27 14:08:19 |
合計ジャッジ時間 | 13,403 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 16 ms
6,400 KB |
testcase_04 | AC | 6 ms
5,376 KB |
testcase_05 | AC | 14 ms
6,272 KB |
testcase_06 | AC | 4 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 5 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 12 ms
5,660 KB |
testcase_13 | AC | 134 ms
33,700 KB |
testcase_14 | AC | 344 ms
66,416 KB |
testcase_15 | AC | 93 ms
28,044 KB |
testcase_16 | AC | 142 ms
35,784 KB |
testcase_17 | AC | 105 ms
29,140 KB |
testcase_18 | AC | 235 ms
47,296 KB |
testcase_19 | AC | 501 ms
84,236 KB |
testcase_20 | AC | 282 ms
59,876 KB |
testcase_21 | AC | 385 ms
72,576 KB |
testcase_22 | AC | 60 ms
20,404 KB |
testcase_23 | AC | 20 ms
7,296 KB |
testcase_24 | AC | 249 ms
36,672 KB |
testcase_25 | AC | 76 ms
16,768 KB |
testcase_26 | AC | 521 ms
64,364 KB |
testcase_27 | AC | 406 ms
53,648 KB |
testcase_28 | AC | 504 ms
64,624 KB |
testcase_29 | AC | 467 ms
58,976 KB |
testcase_30 | AC | 228 ms
36,856 KB |
testcase_31 | AC | 225 ms
36,228 KB |
testcase_32 | AC | 505 ms
64,272 KB |
testcase_33 | AC | 556 ms
69,688 KB |
testcase_34 | AC | 405 ms
54,252 KB |
testcase_35 | AC | 248 ms
38,440 KB |
testcase_36 | AC | 314 ms
46,108 KB |
testcase_37 | AC | 183 ms
31,612 KB |
testcase_38 | AC | 288 ms
42,236 KB |
testcase_39 | AC | 509 ms
64,604 KB |
testcase_40 | AC | 177 ms
31,420 KB |
testcase_41 | AC | 153 ms
26,888 KB |
testcase_42 | AC | 593 ms
69,672 KB |
testcase_43 | AC | 227 ms
76,476 KB |
testcase_44 | AC | 72 ms
31,968 KB |
testcase_45 | AC | 126 ms
46,772 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...);} template<typename S,typename T,typename U> struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){} bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}}; template<typename S,typename T,typename U> auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";} typedef pair<int, int> P; typedef pair<ll, ll> pll; typedef TRI<int, int, int> tri; 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(const int _from, const int _to) noexcept : from(_from), to(_to), sz(0), value(id1), al(id2), lazy(id1), left(nullptr), right(nullptr), par(nullptr){} BSTNode(const int _from, const int _to, const _Tp& _value) noexcept : from(_from), to(_to), sz(1), value(_value), al(value), 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) noexcept { arg1 += arg2; } static _Tp opr2(const _Tp arg1, const _Tp arg2) noexcept { return arg1 + arg2; } inline bool IsRoot() const noexcept { return !par; } inline bool IsVertex() const noexcept { return from == to; } void push() noexcept { if(lazy != id1){ if(IsVertex()) opr1(value, lazy); al += lazy * sz; if(left) opr1(left->lazy, lazy); if(right) opr1(right->lazy, lazy); lazy = id1; } } void eval() noexcept { sz = IsVertex(), 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(const bool right_) noexcept { 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 pair_hash { template <class T1, class T2> size_t operator() (const pair<T1, T2>& p) const { size_t lhs = hash<T1>()(p.first), rhs = hash<T2>()(p.second); return lhs^(rhs+0x9e3779b9+(lhs<<6)+(lhs>>2)); } }; BSTNode<T>** vertex_set; unordered_map<pair<int, int>, pair<BSTNode<T>*, BSTNode<T>*>, pair_hash> edge_set; BSTNode<T> *reroot(BSTNode<T> *ver) noexcept { BSTNode<T> *res = splay(ver)->left; if(!res) return ver; ver->left = nullptr, ver->eval(); while(ver->right) ver->push(), ver = ver->right; splay(ver), ver->right = res, ver->eval(), res->par = ver; return ver; } void link(BSTNode<T> *ver1, BSTNode<T> *ver2) noexcept { BSTNode<T>* e1 = new BSTNode<T>(ver1->from, ver2->from); BSTNode<T>* e2 = new BSTNode<T>(ver2->from, ver1->from); edge_set[{ver1->from, ver2->from}] = {e1, e2}; join(join(reroot(ver1), e1), join(reroot(ver2), e2)); } void cut(BSTNode<T> *edge1, BSTNode<T> *edge2) noexcept { splay(edge1), splay(edge2); BSTNode<T> *p = edge1->par; bool _right = (edge1 == edge2->right); if(p != edge2){ _right = (p == edge2->right); p->par = nullptr, edge1->rotate((edge1 == p->left)); } if(edge1->left) edge1->left->par = nullptr; if(edge1->right) edge1->right->par = nullptr; if(_right){ if(edge2->left) edge2->left->par = nullptr; join(edge2->left, edge1->right); }else{ if(edge2->right) edge2->right->par = nullptr; join(edge1->left, edge2->right); } // delete edge1; delete edge2; } bool IsConnected(BSTNode<T> *ver1, BSTNode<T> *ver2) noexcept { splay(ver1), splay(ver2); return ver1->par; } T& get(BSTNode<T> *ver) noexcept { return splay(ver)->value; } void range(BSTNode<T> *edge1, BSTNode<T> *edge2, const T& val) noexcept { 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) noexcept { 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; } void dfs(const int u, const int p, const BSTNode<T> *cur, bool *visit, vector<BSTNode<T>*>& nodes, const vector<vector<int> >& G) noexcept { visit[u] = true; nodes.push_back(vertex_set[u]); for(auto& v : G[u]){ if(v != p){ BSTNode<T>* e1 = new BSTNode<T>(u, v); nodes.push_back(e1); dfs(v, u, cur, visit, nodes, G); BSTNode<T>* e2 = new BSTNode<T>(v, u); if(u < v) edge_set[{u, v}] = {e1, e2}; else edge_set[{v, u}] = {e2, e1}; nodes.push_back(e2); } } } void bst_build(vector<BSTNode<T>* >& nodes) noexcept { int i, n = (int)nodes.size(), st = 2, isolate = ((n % 4 == 1) ? (n-1) : -1); while(st <= n){ for(i = st-1; i < n; i += 2*st){ nodes[i]->left = nodes[i-st/2], nodes[i-st/2]->par = nodes[i]; if(i+st/2 < n) nodes[i]->right = nodes[i+st/2], nodes[i+st/2]->par = nodes[i]; else if(isolate >= 0) nodes[i]->right = nodes[isolate], nodes[isolate]->par = nodes[i]; nodes[i]->eval(); } isolate = ((n % (4*st) >= st && (n % (4*st) < 2*st)) ? (i-2*st): isolate); st <<= 1; } } void build_forest(const vector<vector<int> >& forest) noexcept { bool *visit = new bool[V](); for(int i = 0; i < V; i++){ if(!visit[i]){ vector<BSTNode<T>* > nodes; BSTNode<T> *cur = nullptr; dfs(i, -1, cur, visit, nodes, forest); bst_build(nodes); } } delete[] visit; } void build_tree(const int root, const vector<vector<int> >& tree) noexcept { bool *visit = new bool[V](); vector<BSTNode<T>* > nodes; BSTNode<T> *cur = nullptr; dfs(root, -1, cur, visit, nodes, tree); bst_build(nodes); delete[] visit; } public: const int V; EulerTourTree(const vector<T>& ver_value) noexcept : V((int)ver_value.size()){ vertex_set = new BSTNode<T>*[V]; for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode<T>(i, i, ver_value[i]); } EulerTourTree(const vector<T>& ver_value, const vector<vector<int> >& forest) noexcept : EulerTourTree(ver_value){ build_forest(forest); } EulerTourTree(const vector<T>& ver_value, const int root, const vector<vector<int> >& tree) noexcept : EulerTourTree(ver_value){ build_tree(root, tree); } // ~EulerTourTree(){ // for(auto it : edge_set){ // delete (it.second).first; // delete (it.second).second; // } // for(int i = 0; i < V; ++i) delete vertex_set[i]; // delete[] vertex_set; // } // 根を node_id にする void reroot(const int node_id) noexcept { reroot(vertex_set[node_id]); } // 辺(node1_id, node2_id) を追加 void link(int node1_id, int node2_id) noexcept { if(node1_id > node2_id) swap(node1_id, node2_id); link(vertex_set[node1_id], vertex_set[node2_id]); } // 辺(node1_id, node2_id) を削除(逆向きでも問題ない) void cut(int node1_id, int node2_id){ if(node1_id > node2_id) swap(node1_id, node2_id); auto it = edge_set.find({node1_id, node2_id}); assert(it != edge_set.end()); BSTNode<T> *edge1 = (it->second).first, *edge2 = (it->second).second; edge_set.erase(it); cut(edge1, edge2); } // node1_id と node2_id が同じ木(連結成分)に属するか bool IsConnected(const int node1_id, const int node2_id) noexcept { if(node1_id == node2_id) return true; return IsConnected(vertex_set[node1_id], vertex_set[node2_id]); } // 頂点 ver_id の値を取得 T& get(int ver_id) noexcept { return get(vertex_set[ver_id]); } // 頂点 ver_id の存在する連結成分内の頂点全体に val を加える void component_range(const int ver_id, const T& val){ range(ver_id, -1, val); } // 親が par_id であるような頂点 ver_id の部分木内に存在する頂点全体に val を加える void range(const int ver_id, const int par_id, const T& val){ if(par_id < 0) return BSTNode<T>::opr1(splay(vertex_set[ver_id])->lazy, val); if(ver_id < par_id){ auto it = edge_set.find({ver_id, par_id}); assert(it != edge_set.end()); range((it->second).second, (it->second).first, val); }else{ auto it = edge_set.find({par_id, ver_id}); assert(it != edge_set.end()); range((it->second).first, (it->second).second, val); } } // 頂点 ver_id の存在する連結成分内の頂点全体の総和を取得 T component_query(const int ver_id){ return query(ver_id, -1); } // 親が par_id であるような頂点 ver_id の部分木内に存在する頂点全体の総和を取得 T query(const int ver_id, const int par_id){ if(par_id < 0) return splay(vertex_set[ver_id])->al; if(ver_id < par_id){ auto it = edge_set.find({ver_id, par_id}); assert(it != edge_set.end()); return query((it->second).second, (it->second).first); }else{ auto it = edge_set.find({par_id, ver_id}); assert(it != edge_set.end()); return query((it->second).first, (it->second).second); } } private: void check_dfs(const BSTNode<T>* cur) const noexcept { cur->push(); if(cur->left) check_dfs(cur->left); if(cur->IsVertex()) cout <<"{" << (cur->from) << "," << (cur->value) << "} "; if(cur->right) check_dfs(cur->right); } }; #define getchar getchar_unlocked #define putchar putchar_unlocked inline int in() { int n = 0; short c; while ((c = getchar()) >= '0') n = n * 10 + c - '0'; return n; } inline void out(int n) { short res[10], i = 0; do { res[i++] = n % 10, n /= 10; } while (n); while (i) putchar(res[--i] + '0'); putchar('\n'); } int r[MAX_N]; struct SW { chrono::high_resolution_clock::time_point st,ed; void start() { st = chrono::high_resolution_clock::now(); } void end() { ed = chrono::high_resolution_clock::now(); cout << chrono::duration_cast<chrono::milliseconds>(ed - st).count() << " msec" << endl; } }; int main() { int n = in(); rep(i,n) r[i] = in(); vector<vector<int> > G(n); rep(i,n-1){ int u = in(), v = in(); G[u-1].push_back(v-1), G[v-1].push_back(u-1); } EulerTourTree<int> et(vi(n, 0), 0, G); rrep(i,n){ et.component_range(i, 1); for(auto& to : G[i]){ if(to < i) et.cut(to, i); } } int ans = 1; rep(i,n){ ans = (ll)ans * (et.get(i) + r[i]) % MOD; } out(ans); return 0; }