Created
December 15, 2016 13:37
-
-
Save sntea-hogex/2622f3016f0d316048b64712fa3894dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef LOCAL111 | |
#define _GLIBCXX_DEBUG | |
#else | |
#define NDEBUG | |
#endif | |
#include <bits/stdc++.h> | |
const long long INF = 1e17; | |
using namespace std; | |
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; } | |
//library | |
typedef long long cost_t; | |
class Edge { | |
public: | |
int to; | |
cost_t cost; | |
Edge(){ | |
} | |
Edge(int x,cost_t y){ | |
to = x; | |
cost = y; | |
} | |
bool operator< (const Edge& x) const { | |
if(cost != x.cost) return cost < x.cost; | |
return to < x.to; | |
} | |
bool operator> (const Edge& x) const { | |
if(cost != x.cost) return cost > x.cost; | |
return to > x.to; | |
} | |
}; | |
class Graph { | |
private: | |
//const long long int INF = (long long)1e18; | |
vector<vector<Edge> > v; | |
int n; | |
public: | |
Graph(int x){ | |
n = x; | |
v = vector<vector<Edge> >(x); | |
} | |
vector<Edge>& operator[](int x){ | |
return v[x]; | |
} | |
const vector<Edge>& operator[](int x) const { | |
return v[x]; | |
} | |
int size() const { | |
return n; | |
} | |
void add_edge(int from, Edge e){ | |
v[from].push_back(e); | |
} | |
void add_edge(int from, int to, cost_t cost){ | |
add_edge(from,Edge(to,cost)); | |
} | |
void add_uedge(int from, int to, cost_t cost){ | |
add_edge(from,to,cost); | |
add_edge(to,from,cost); | |
} | |
}; | |
//liblary | |
#define endl '\n' | |
#define ALL(a) (a).begin(),(a).end() | |
#define SZ(a) int((a).size()) | |
#define FOR(i,a,b) for(int i=(a);i<(b);++i) | |
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) | |
#define REP(i,n) FOR(i,0,n) | |
#define RREP(i,n) for (int i=(n)-1;i>=0;i--) | |
#ifdef LOCAL111 | |
#define DEBUG(x) cout<<#x<<": "<<(x)<<endl | |
template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} | |
#else | |
#define DEBUG(x) true | |
template<typename T> void dpite(T a, T b){ return; } | |
#endif | |
#define F first | |
#define S second | |
#define SNP string::npos | |
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": " | |
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} | |
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} | |
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} | |
typedef long long int LL; | |
typedef unsigned long long ULL; | |
typedef pair<int,int> P; | |
typedef pair<LL,LL> LP; | |
void ios_init(){ | |
//cout.setf(ios::fixed); | |
//cout.precision(12); | |
#ifdef LOCAL111 | |
return; | |
#endif | |
ios::sync_with_stdio(false); cin.tie(0); | |
} | |
int main() | |
{ | |
ios_init(); | |
int n; | |
while(cin >> n){ | |
Graph g(n); | |
vector<LL> a(n); | |
REP(i,n) cin >> a[i]; | |
REP(i,n-1){ | |
int a,b; | |
cin >> a >> b; | |
a--; | |
g.add_uedge(i+1,a,b); | |
} | |
int cnt = 1; | |
{ | |
int n_ = n; | |
while(n_ != 0) n_>>=1, cnt++; | |
} | |
vector<vector<LL>> db(n,vector<LL>(cnt,-1)); | |
vector<vector<LL>> dbc(n,vector<LL>(cnt,INF)); | |
function<void(int,int,LL)> rec = [&](int p, int par, LL cost){ | |
if(par != -1){ | |
db[p][0] = par; | |
dbc[p][0] = cost; | |
FOR(i,1,cnt){ | |
if(db[db[p][i-1]][i-1] != -1){ | |
db[p][i] = db[db[p][i-1]][i-1]; | |
dbc[p][i] = dbc[p][i-1]+dbc[db[p][i-1]][i-1]; | |
}else break; | |
} | |
} | |
for(auto&& e : g[p]) { | |
if(e.to != par){ | |
rec(e.to, p, e.cost); | |
} | |
} | |
return; | |
}; | |
rec(0,-1,-1); | |
vector<int> imos(n,0); | |
REP(i,n){ | |
imos[i]++; | |
int p = i; | |
LL v = a[i]; | |
RREP(k,cnt){ | |
if(v >= dbc[p][k]){ | |
v -= dbc[p][k]; | |
p = db[p][k]; | |
} | |
} | |
if(p != 0){ | |
imos[db[p][0]]--; | |
} | |
} | |
function<void(int,int)> dfs = [&](int p, int par){ | |
for(auto&& e : g[p]) if(e.to != par){ | |
dfs(e.to,p); | |
imos[p] += imos[e.to]; | |
} | |
}; | |
dfs(0,-1); | |
REP(i,n) imos[i]--; | |
pite(ALL(imos)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment