#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5;
ll n, vl[nx], u, v, w, used[nx], sz[nx], ans, t;
queue<ll> q;
vector<pair<ll, ll>> d[nx];
map<ll, int> mp;
struct fenwick
{
ll d[nx];
void add(int i, int vl)
{
while (i<nx) d[i]+=vl, i+=(i&-i);
}
ll query(int i)
{
ll res=0;
while (i>0) res+=d[i], i-=(i&-i);
return res;
}
} f;
int dfssz(int u, int p)
{
sz[u]=1;
for (auto [v, w]:d[u]) if (v!=p&&!used[v]) sz[u]+=dfssz(v, u);
return sz[u];
}
int findcentroid(int u, int p, int rtsz)
{
for (auto [v, w]:d[u]) if (v!=p&&!used[v]&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
return u;
}
void dfsquery(int u, int p, int rt, ll sm, bool mode)
{
if (sm>=0)
{
//if (mode) cout<<u<<' '<<p<<' '<<sm<<' '<<sm+vl[rt]<<' '<<f.query(mp[sm+vl[rt]])<<'\n';
if (mode) ans+=f.query(mp[sm+vl[rt]]);
else mp[sm+vl[rt]]=0;
}
for (auto [v, w]:d[u]) if (v!=p&&!used[v]) dfsquery(v, u, rt, min(0ll, sm)+vl[v]-w, mode);
}
void dfsadd(int u, int p, ll w, ll cost, ll left, bool mode, int add)
{
//if (u==8&&p==6) cout<<"here "<<w<<' '<<'\n';
if (w>left) cost+=(w-left), left=0;
else left-=w;
if (mode) f.add(mp[cost], add);
else mp[cost]=0;
for (auto [v, w]:d[u]) if (v!=p&&!used[v]) dfsadd(v, u, w, cost, vl[u]+left, mode, add);
}
void decomposition(int u)
{
u=findcentroid(u, u, dfssz(u, u));
used[u]=1;
mp.clear();
mp[0]=0;
mp[vl[u]]=0;
t=0;
for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 0, 0), dfsquery(v, u, u, -w+vl[v], 0);
for (auto &[x, y]:mp) y=++t;
f.add(mp[0], 1);
for (int i=0; i<d[u].size(); i++)
{
auto [v, w]=d[u][i];
if (used[v]) continue;
//cout<<"order "<<u<<' '<<v<<'\n';
dfsquery(v, u, u, -w+vl[v], 1);
dfsadd(v, u, w, 0, 0, 1, 1);
}
f.add(mp[0], -1);
for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 1, -1);
for (int i=d[u].size()-1; i>=0; i--)
{
auto [v, w]=d[u][i];
if (used[v]) continue;
//cout<<"order "<<u<<' '<<v<<'\n';
dfsquery(v, u, u, -w+vl[v], 1);
dfsadd(v, u, w, 0, 0, 1, 1);
}
ans+=f.query(mp[vl[u]]);
//cout<<"ans "<<u<<' '<<ans<<'\n';
for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 1, -1);
for (auto [v, w]:d[u]) if (!used[v]) decomposition(v);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>vl[i];
for (int i=1; i<n; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w});
decomposition(1);
cout<<ans;
}
/*
5
3 1 2 4 5
1 2 3
3 2 2
4 2 6
5 4 3
8
5 2 4 7 8 3 3 6
6 5 5
1 4 5
3 1 2
8 6 5
1 2 3
4 5 3
4 7 5
*/
Compilation message
transport.cpp: In function 'void decomposition(int)':
transport.cpp:74:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i=0; i<d[u].size(); i++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
194 ms |
11864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
229 ms |
13908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
342 ms |
17744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
7844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
139 ms |
10144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
281 ms |
11508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
399 ms |
13472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
565 ms |
15704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |