This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |