#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 1e5 + 5;
template <class T> using Tree = tree<T,null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
int a[N];
int sz[N];
bool block[N];
long long sum[N];
vector<pair<int, int>> g[N];
long long min_up[N], min_down[N];
void find_sizes(int v, int p) {
sz[v] = 1;
for (auto [u, w] : g[v]) {
if (!block[u] && u != p) {
find_sizes(u, v);
sz[v] += sz[u];
}
}
}
int find_centroid(int v, int p, int x) {
for (auto [u, w] : g[v]) {
if (!block[u] && u != p && sz[u] > x / 2) {
return find_centroid(u, v, x);
}
}
return v;
}
long long dfs_qry(int v, int p, Tree<int>& se, const int centroid, const bool rep) {
long long ret = 0;
if (min_down[v] >= 0 && !rep) {
ret++;//centroid -> v
}
if (min_up[v] >= 0) {
ret += (int) se.size() - se.order_of_key(-(sum[v] - a[centroid]));
}
for (auto [u, w] : g[v]) {
if (u == p || block[u]) {
continue;
}
sum[u] = sum[v] + a[u] - w;
min_up[u] = min(0LL, min_up[v] + a[u] - w);
min_down[u] = min(min_down[v], sum[v] - w);
ret += dfs_qry(u, v, se, centroid, rep);
}
return ret;
}
void dfs_upd(int v, int p, Tree<int>& se) {
se.insert(min_down[v]);
for (auto [u, w] : g[v]) {
if (u != p && !block[u]) {
dfs_upd(u, v, se);
}
}
}
long long decompose(int node) {
find_sizes(node, node);
int centroid = find_centroid(node, node, sz[node]);
block[centroid] = true;
long long ret = 0;
for (int rep = 0; rep < 2; ++rep) {
Tree<int> se;
if (rep == 0) {
se.insert(0);
}
min_down[centroid] = min_up[centroid] = 0;
sum[centroid] = a[centroid];
for (auto [u, w] : g[centroid]) {
if (block[u]) {
continue;
}
sum[u] = sum[centroid] + a[u] - w;
min_up[u] = min(0, a[u] - w);
min_down[u] = min(0, a[centroid] - w);
ret += dfs_qry(u, centroid, se, centroid, rep);
dfs_upd(u, centroid, se);
}
reverse(g[centroid].begin(), g[centroid].end());
}
for (auto [u, w] : g[centroid]) {
if (!block[u]) {
ret += decompose(u);
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
long long ans = decompose(1);
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4956 KB |
Output is correct |
2 |
Correct |
6 ms |
5104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
5368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
142 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
206 ms |
16212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
323 ms |
20304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
7688 KB |
Output is correct |
2 |
Correct |
45 ms |
7560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
10076 KB |
Output is correct |
2 |
Correct |
141 ms |
9612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
11988 KB |
Output is correct |
2 |
Correct |
178 ms |
12368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
14160 KB |
Output is correct |
2 |
Correct |
255 ms |
14272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
17236 KB |
Output is correct |
2 |
Correct |
391 ms |
16048 KB |
Output is correct |