#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e5 + 5;
int n, a[maxn], w[maxn];
vector <pair<int, long long>> g[maxn];
int used[maxn];
long long dp1[maxn], ben1[maxn];
long long dp2[maxn], ben2[maxn];
int sz[maxn];
long long answ;
void sizeCounter(int v, int p) {
sz[v] = 1;
dp1[v] = dp2[v] = 0;
ben1[v] = ben2[v] = 0;
for (size_t i = 0; i < g[v].size(); i++) {
int to = g[v][i].first;
if (to == p || used[to]) continue;
sizeCounter(to, v);
sz[v] += sz[to];
}
}
int center = 0;
void findCen(int v, int p) {
int cnt = sz[v] / 2 + 1;
for (size_t i = 0; i < g[v].size(); i++) {
int to = g[v][i].first;
if (to == p || used[to]) continue;
if (sz[to] > cnt) {
center = to;
findCen(to, v);
}
}
}
vector <long long> G;
void push_DP(int v, int p) {
sz[v] = 1;
for (size_t i = 0; i < g[v].size(); i++) {
int to = g[v][i].first;
int len = g[v][i].second;
if (to == p || used[to]) continue;
ben1[to] = ben1[v] + a[v] - len;
dp1[to] = dp1[v];
if (ben1[to] < 0) {
dp1[to] -= ben1[to];
ben1[to] = 0;
}
int d = a[to] - len;
dp2[to] = dp2[v] + d;
ben2[to] = min(1ll * d, ben2[v] + d);
push_DP(to, v);
sz[v] += sz[to];
}
if (ben2[v] >= 0) {
G.push_back(dp2[v]);
}
}
int findCenter(int v) {
sizeCounter(v, -1);
center = v;
findCen(v, -1);
push_DP(center, -1);
return center;
}
vector <long long> P;
vector <int> ver;
void solve(int v, int p) {
if (p != -1) {
ver.push_back(v);
if (ben2[v] >= 0) {
P.push_back(dp2[v]);
}
}
int idx = lower_bound(G.begin(), G.end(), dp1[v]) - G.begin();
answ += (int)G.size() - idx;
if (p == -1) answ--;
for (size_t i = 0; i < g[v].size(); i++) {
int to = g[v][i].first;
if (to == p || used[to]) continue;
solve(to, v);
if (p == -1) {
sort(P.begin(), P.end());
for (size_t j = 0; j < ver.size(); j++) {
int u = ver[j];
int id = lower_bound(P.begin(), P.end(), dp1[u]) - P.begin();
answ -= (int)P.size() - id;
}
P.clear();
ver.clear();
}
}
}
void centroidDecomposition() {
queue <int> q;
q.push(1);
while (!q.empty()) {
int v = q.front();
q.pop();
v = findCenter(v);
sort(G.begin(), G.end());
solve(v, -1);
G.clear();
for (size_t i = 0; i < g[v].size(); i++) {
int to = g[v][i].first;
if (used[to] || sz[to] == 1) continue;
q.push(to);
}
used[v] = 1;
}
}
inline void sc(int& x) {
register int c;
x = 0;
do {
c = getchar();
} while (c < '0');
do {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
} while (c >= '0');
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
sc(n);
for (size_t i = 1; i <= n; i++)
sc(a[i]);
for (size_t i = 0; i < n - 1; i++) {
int x, y, z;
sc(x); sc(y); sc(z);
g[x].push_back({ y, z });
g[y].push_back({ x, z });
}
centroidDecomposition();
cout << answ << '\n';
return 0;
}
Compilation message
transport.cpp: In function 'int main()':
transport.cpp:153:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (size_t i = 1; i <= n; i++)
~~^~~~
transport.cpp:155:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (size_t i = 0; i < n - 1; i++) {
~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3064 KB |
Output is correct |
2 |
Correct |
9 ms |
3192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
3556 KB |
Output is correct |
2 |
Correct |
251 ms |
3680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1054 ms |
11232 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
14112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1066 ms |
18160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
920 ms |
6140 KB |
Output is correct |
2 |
Correct |
45 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
7288 KB |
Output is correct |
2 |
Correct |
591 ms |
7120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
8992 KB |
Output is correct |
2 |
Correct |
192 ms |
8956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
10920 KB |
Output is correct |
2 |
Correct |
303 ms |
10740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1042 ms |
13580 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |