#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 = {-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<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 |
Incorrect |
8 ms |
2900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
3400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
151 ms |
9268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
153 ms |
11016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
221 ms |
14304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
129 ms |
5592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
104 ms |
7112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
242 ms |
8104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
337 ms |
9396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
437 ms |
11260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |