1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using llf = long double;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXN = 100005;

struct strongly_connected {
	vector<vector<int>> gph;

	void init(int n) {
		gph.clear();
		gph.resize(n);
	}

	void add_edge(int s, int e) { gph[s].push_back(e); }

	vector<int> val, comp, z, cont;
	int Time, ncomps;
	template <class G, class F> int dfs(int j, G &g, F f) {
		int low = val[j] = ++Time, x;
		z.push_back(j);
		for (auto e : g[j])
			if (comp[e] < 0)
				low = min(low, val[e] ?: dfs(e, g, f));

		if (low == val[j]) {
			do {
				x = z.back();
				z.pop_back();
				comp[x] = ncomps;
				cont.push_back(x);
			} while (x != j);
			f(cont);
			cont.clear();
			ncomps++;
		}
		return val[j] = low;
	}
	template <class G, class F> void scc(G &g, F f) {
		int n = sz(g);
		val.assign(n, 0);
		comp.assign(n, -1);
		Time = ncomps = 0;
		for (int i = 0; i < n; i++)
			if (comp[i] < 0)
				dfs(i, g, f);
	}

	int piv;
	void get_scc(int n) {
		scc(gph, [&](vector<int> &v) {});
		for (int i = 0; i < n; i++) {
			comp[i] = ncomps - comp[i] - 1;
		}
		piv = ncomps;
	}
} scc;

bool vis[MAXN];
vector<pi> gph[MAXN];
vector<lint> a;

vector<pi> src, snk;

void dfs(int x, int p, lint d = 0) {
	snk.push_back({d, x});
	if (a[x] - d >= 0)
		src.push_back({a[x] - d, x});
	for (auto &[w, y] : gph[x]) {
		if (y == p || vis[y])
			continue;
		dfs(y, x, d + w);
	}
}

int sz[MAXN], msz[MAXN];
vector<int> dfn;

void dfsc(int x, int p) {
	sz[x] = 1;
	msz[x] = 0;
	dfn.push_back(x);
	for (auto &[_, y] : gph[x]) {
		if (y == p || vis[y])
			continue;
		dfsc(y, x);
		sz[x] += sz[y];
		msz[x] = max(msz[x], sz[y]);
	}
}

int get_center(int x) {
	dfn.clear();
	dfsc(x, -1);
	pi ret{int(1e9), -1};
	for (auto &y : dfn) {
		int w = max(msz[y], sz(dfn) - sz[y]);
		ret = min(ret, {w, y});
	}
	return ret[1];
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cout.precision(69);
	int n;
	cin >> n;
	a.resize(n);
	for (auto &x : a)
		cin >> x;
	for (int i = 0; i < n - 1; i++) {
		lint u, v, w;
		cin >> u >> v >> w;
		u--;
		v--;
		gph[u].push_back({w, v});
		gph[v].push_back({w, u});
	}
	queue<int> que;
	que.push(0);
	int nvtx = n;
	vector<pi> edges;
	auto addEdge = [&](int u, int v) { edges.push_back({u, v}); };
	while (sz(que)) {
		auto x = que.front();
		que.pop();
		x = get_center(x);
		src.clear();
		snk.clear();
		dfs(x, -1);
		sort(all(src));
		sort(all(snk));
		for (int i = 0; i < sz(snk); i++) {
			if (i + 1 < sz(snk))
				addEdge(nvtx + i + 1, nvtx + i);
			addEdge(nvtx + i, snk[i][1]);
		}
		int j = 0;
		for (int i = 0; i < sz(src); i++) {
			while (j < sz(snk) && snk[j][0] <= src[i][0]) {
				j++;
			}
			if (j)
				addEdge(src[i][1], nvtx + j - 1);
		}
		nvtx += sz(snk);
		vis[x] = 1;
		for (auto &[_, y] : gph[x]) {
			if (!vis[y])
				que.push(y);
		}
	}
	scc.init(nvtx);
	for (auto &[u, v] : edges)
		scc.add_edge(u, v);
	scc.get_scc(nvtx);
	vector<int> relevant(scc.piv);
	vector<bitset<MAXN>> dp(scc.piv);
	for (int i = 0; i < n; i++) {
		relevant[scc.comp[i]] = 1;
		dp[scc.comp[i]].set(i);
	}
	vector<pi> ne;
	for (auto &[u, v] : edges) {
		if (scc.comp[u] != scc.comp[v]) {
			ne.push_back({scc.comp[u], scc.comp[v]});
		}
	}
	sort(all(ne));
	for (auto &[u, v] : ne)
		relevant[v] |= relevant[u];
	reverse(all(ne));
	for (auto &[u, v] : ne) {
		if (relevant[u])
			dp[u] |= dp[v];
	}
	for (int i = 0; i < n; i++) {
		cout << dp[scc.comp[i]].count() << " ";
	}
	cout << "\n";
}