#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,lli> pii;
#define dbg(x) cout<<#x<<" has a value of: "<<x<<"\n";
#define usize(x) (int)(x.size())
#define plist(x) for(int i=0;i<usize(x);i++)cout<<"eleman "<<i<<" = "<<x[i]<<"\n";
#define foreach(x) for(auto item:x)
#define fill(s,x) for(int i=0;i<x;i++)cin>>s[i];
#define btw(x,y,z) x>=y && x<=z
std::ostream& operator<<(std::ostream& os, pair<int,int> p) {
os << p.first << ", " << p.second;
return os;
}
vector<vector<pii> > adj;
vector<lli> dists;
vector<pii> cvbs;
void dfs1(int node,int parent){
foreach(adj[node]){
if(item.first==parent){
continue;
}
dists[item.first]=dists[node]+item.second;
dfs1(item.first,node);
}
}
int n,m;
pii dfs2(int node,int parent){
if(btw(node,n+1,n+m)){
return pii(dists[node],0);
}
vector<pii> sncler;
foreach(adj[node]){
if(item.first==parent){
continue;
}
pii s1=dfs2(item.first,node);
sncler.push_back(s1);
}
sort(sncler.begin(),sncler.end());
pii don(0,0);
int nn=sncler.size();
don.first=sncler[nn/2-(1-nn%2)].first;
//dbg(don.first);
foreach(sncler){
don.second+=item.second;
don.second+=abs(item.first-don.first);
}
//dbg(don);
return don;
}
int main(){
cin>>n>>m;
adj=vector<vector<pii> >(n+m+1,vector<pii>());
dists=vector<lli>(n+m+1,0);
for(int i=2;i<=n+m;i++){
int a,b;
cin>>a>>b;
adj[i].push_back(pii(a,b));
adj[a].push_back(pii(i,b));
}
dfs1(1,-1);
cout<<dfs2(1,-1).second<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
708 KB |
Output is correct |
10 |
Correct |
2 ms |
708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
708 KB |
Output is correct |
10 |
Correct |
2 ms |
708 KB |
Output is correct |
11 |
Incorrect |
2 ms |
756 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
524 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
656 KB |
Output is correct |
8 |
Correct |
2 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
708 KB |
Output is correct |
10 |
Correct |
2 ms |
708 KB |
Output is correct |
11 |
Incorrect |
2 ms |
756 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |