#include <bits/stdc++.h>
using namespace std;
int sz[200007];long long all = 0;
int vis[200007];
vector<pair<int,int>> adj[200007];
bool ss = 0;
long long cost[200007];
vector<int> bs;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<long long,int>, null_type,less<pair<long long,int>>, rb_tree_tag,tree_order_statistics_node_update>
ordered_set s;
vector<pair<int,int>> ch;
void calc(int no,int pr,long long dep,long long mi){
if(!ss){//ending
pair<long long,int> p = {-dep,0};
long long it = s.order_of_key(p);
all+=s.size()-it;
}else{//starting (reverse)
if(dep>=mi){
s.insert({dep,no});
mi = dep;
}
}
for(auto i:adj[no]){
if(i.first==pr||vis[i.first])continue;
calc(i.first,no,dep+((ss==1?cost[i.first]:cost[no])-i.second),mi);
}
}
int dfs(int no,int pr){
sz[no] = 1;
for(auto i:adj[no]){
if(i.first==pr||vis[i.first]!=0)continue;
sz[no]+=dfs(i.first,no);
}
return sz[no];
}
int get_centroid(int no,int ss,int pr){
for(auto i:adj[no]){
if(pr==i.first||vis[i.first]!=0)continue;
if(sz[i.first]*2>ss){
return get_centroid(i.first,ss,no);
}
}
return no;
}
void centroid(int no){
int cen = get_centroid(no,dfs(no,-1),-1);
vis[cen] = 1;
//cout<<cen<<"\n";
s.clear();ch.clear();
s.insert({0,cen});
for(auto i:adj[cen]){
if(vis[i.first])continue;
ss = 0;
calc(i.first,-1,((ss==1?cost[i.first]:cost[cen])-i.second),0);
ss = 1;
calc(i.first,-1,((ss==1?cost[i.first]:cost[cen])-i.second),0);
ch.push_back({i.first,i.second});
}
//cout<<all<<"h"<<endl;
s.clear();
while(!ch.empty()){
ss = 0;
calc(ch.back().first,-1,((ss==1?cost[ch.back().first]:cost[cen])-ch.back().second),0);
ss = 1;
calc(ch.back().first,-1,((ss==1?cost[ch.back().first]:cost[cen])-ch.back().second),0);
ch.pop_back();
}
pair<int,int> p = {0,0};
int it = s.order_of_key(p);
all+=s.size()-it;
//cout<<all<<endl;
for(auto i:adj[cen]){
if(vis[i.first])continue;
centroid(i.first);
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i = 1;i<=n;i++){
cin>>cost[i];
}
for(int i = 0;i<n-1;i++){
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
centroid(1);
cout<<all<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
5588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
155 ms |
11912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
13696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
207 ms |
17172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
136 ms |
7932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
104 ms |
9840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
270 ms |
10828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
317 ms |
12236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
495 ms |
14276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |