#include <bits/stdc++.h>
using namespace std;
int sz[100007];long long all = 0;
int vis[100007];
vector<pair<long long,long long>> adj[100007];
bool ss = 0;
long long cost[100007];
#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,long long>, null_type,less<pair<long long,long long>>, 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,long long> p = {-min(mi,dep),0};
long long it = s.order_of_key(p);
all+=s.size()-it;
}else{//starting (reverse)
if(dep>=mi){
s.insert({dep,no});
}
}
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),(ss?max(mi,dep):min(mi,dep)));
}
}
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<long long,long long> p = {0,0};
long long 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++){
long long a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
centroid(1);
cout<<all;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2936 KB |
Output is correct |
2 |
Correct |
7 ms |
3080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3284 KB |
Output is correct |
2 |
Correct |
9 ms |
3428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
9072 KB |
Output is correct |
2 |
Correct |
64 ms |
8524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
10656 KB |
Output is correct |
2 |
Correct |
124 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
14040 KB |
Output is correct |
2 |
Correct |
160 ms |
17740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
5412 KB |
Output is correct |
2 |
Correct |
41 ms |
5168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
7032 KB |
Output is correct |
2 |
Correct |
119 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
7860 KB |
Output is correct |
2 |
Correct |
155 ms |
8992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
9124 KB |
Output is correct |
2 |
Correct |
302 ms |
10612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
11088 KB |
Output is correct |
2 |
Correct |
278 ms |
11624 KB |
Output is correct |