#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +100500
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e9)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
vector <ll> vc;
ll t[N], cnt[N], ans;
int siz[N], i, n, u, v, w, kol;
vector <pair <int, ll> > g[N];
bool mk[N];
void upd(int v)
{
int new_v = upper_bound(vc.begin(), vc.end(), v) - vc.begin() - 1;
v = new_v;
for (; v < N; v = (v | (v + 1))) t[v]++;
}
ll sum(int v)
{
int new_v = upper_bound(vc.begin(), vc.end(), v) - vc.begin() - 1;
v = new_v;
ll cur = 0;
for (; v >= 0; v = (v & (v + 1)) - 1) cur += t[v];
return cur;
}
void addremains(int v, int pr, ll rem, ll remain)
{
rem += cnt[v];
vc.pb(remain);
for (auto u : g[v])
{
int to = u.F;
ll len = u.S;
if (to == pr || mk[to]) continue;
ll new_rem = max(0ll, rem - len );
ll new_remain = remain + max(0ll, len - rem);
addremains(to, v, new_rem, new_remain);
}
}
void calcremains(int v, int pr, ll rem, ll remain)
{
rem += cnt[v];
upd(remain);
for (auto u : g[v])
{
int to = u.F;
ll len = u.S;
if (to == pr || mk[to]) continue;
ll new_rem = max(0ll, rem - len);
ll new_remain = remain + max(0ll, len - rem);
calcremains(to, v, new_rem, new_remain);
}
}
void calcneeds(int v, int pr, ll rem, ll cost, ll need)
{
siz[v] = 1;
need += cost;
need = max(0ll, need - cnt[v]);
rem += cnt[v];
rem -= cost;
if (need == 0)
{
kol++;
ans += sum(rem);
}
for (auto u : g[v])
{
int to = u.F;
ll len = u.S;
if (to == pr || mk[to]) continue;
calcneeds(to, v, rem, len, need);
siz[v] += siz[to];
}
}
void dfs(int v, int pr)
{
siz[v] = 1;
for (auto u : g[v])
{
if (u.F == pr || mk[u.F]) continue;
dfs(u.F, v);
siz[v] += siz[u.F];
}
}
int fnd_cent(int v, int pr)
{
if (pr != -1)
{
siz[pr] -= siz[v];
siz[v] += siz[pr];
}
int cent = -1;
for (auto u : g[v])
{
int to = u.F;
if (mk[to]) continue;
if (siz[to] > siz[v] / 2) {cent = to;break;}
}
if (cent == -1) return v;
return fnd_cent(cent, v);
}
void calc(int v)
{
kol = 0;
mk[v] = 1;
vc.clear();
vc.pb(-1);
for (auto u : g[v])
{
int to = u.F;
ll len = u.S;
if (mk[to]) continue;
addremains(to, -1, 0, len);
}
sort(vc.begin(), vc.end());
vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
for (int i = 0; i < sz(vc); i++) t[i] = 0;
for (auto u : g[v])
{
int to = u.F;
ll len = u.S;
if (mk[to]) continue;
calcneeds(to, -1, cnt[v], len, 0);
calcremains(to, -1, 0, len);
}
for (int i = 0; i < sz(vc); i++) t[i] = 0;
for (int i = sz(g[v]) - 1; i >= 0; i--)
{
int to = g[v][i].F;
ll len = g[v][i].S;
if (mk[to]) continue;
calcneeds(to, -1, cnt[v], len, 0);
calcremains(to, -1, 0, len);
}
ans += kol / 2;
ans += sum(cnt[v]);
for (auto u : g[v])
{
int to = u.F;
if (mk[to]) continue;
dfs(to, -1);
int cent = fnd_cent(to, -1);
if (cent != -1) calc(cent);
}
}
int main()
{
srand(time(0));
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
// in("input.txt");
// out("output.txt");
cin >> n;
for (i = 1; i <= n; i++) cin >> cnt[i];
for (i = 1; i < n; i++)
{
cin >> v >> u >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1, -1);
calc(fnd_cent(1, -1));
cout << ans;
}
/*
8 8
...).).*
*....)..
.)*(..).
(*)((...
.)).)(..
.)(.)..(
...).(.*
M.......
*/
//
//110000
//1100
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2944 KB |
Output is correct |
2 |
Correct |
13 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
3332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
114 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
159 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
220 ms |
12264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
4864 KB |
Output is correct |
2 |
Correct |
53 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
6260 KB |
Output is correct |
2 |
Correct |
183 ms |
6004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
7412 KB |
Output is correct |
2 |
Correct |
254 ms |
7668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
407 ms |
9076 KB |
Output is correct |
2 |
Correct |
357 ms |
8944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
10608 KB |
Output is correct |
2 |
Correct |
442 ms |
10096 KB |
Output is correct |