#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
vector<pair<int, int>> adj[N];
ll ans = 0;
bool vis[N];
int sz[N], a[N], m;
ll now[N], fw[N];
vector<ll> f, c;
int dfs_sz(int v, int p = -1) {
sz[v] = 1;
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
sz[v] += dfs_sz(x, v);
}
return sz[v];
}
int centroid(int v, int p, int s) {
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
if (sz[x] > s) return centroid(x, v, s);
}
return v;
}
void init(int v, int p, ll sum, ll d) {
if (d >= 0) ans++;
f.push_back(-d);
sum += a[v];
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
init(x, v, sum - w, min(d, sum - w));
}
}
void add(int v, int p, ll sum, ll d) {
int idx = upper_bound(c.begin(), c.end(), -d) - c.begin();
for (int i = idx;i <= m;i += i & -i) fw[i]++;
sum += a[v];
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
add(x, v, sum - w, min(d, sum - w));
}
}
void rem(int v, int p, ll sum, ll d) {
int idx = upper_bound(c.begin(), c.end(), -d) - c.begin();
for (int i = idx;i <= m;i += i & -i) fw[i]--;
sum += a[v];
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
rem(x, v, sum - w, min(d, sum - w));
}
}
void cal(int v, int p, ll sum, ll mn) {
if (mn >= 0) {
int idx = upper_bound(c.begin(), c.end(), sum) - c.begin();
for (int i = idx;i > 0;i -= i & -i) ans += fw[idx];
}
for (auto& [x, w] : adj[v]) {
if (x == p || vis[x]) continue;
cal(x, v, sum - w + a[x], min(mn - w + a[x], sum - w + a[x]));
}
}
void decom(int v) {
v = centroid(v, v, dfs_sz(v) / 2);
vis[v] = 1;
init(v, v, 0, 0);
ans--;
// compress
sort(f.begin(), f.end());
for (auto& x : f) {
if (c.empty() || x != c.back()) c.push_back(x);
}
m = c.size();
add(v, v, 0, 0);
for (auto& [x, w] : adj[v]) {
if (vis[x]) continue;
rem(x, v, a[v] - w, min(0, a[v] - w));
cal(x, v, a[x] - w, min(0, a[x] - w));
add(x, v, a[v] - w, min(0, a[v] - w));
}
rem(v, v, 0, 0);
f.clear();
c.clear();
for (auto& [x, w] : adj[v]) {
if (!vis[x]) decom(x);
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i < n;i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
decom(1);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
63 ms |
10716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
12764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
130 ms |
15788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
7124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
8660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
166 ms |
9312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
241 ms |
10956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
313 ms |
12936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |