#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=200005;
const int INF=1e18;
int n;
vector<pair<int,int>> adj[N],adj1[N];
vector<int> rec(N),deep(N);
vector<int> v(N);
pair<int,int> best_pair;
int best=0;
void dfs1(int k,int pa){
for(auto [j,c]:adj[k]){
if(j==pa)continue;
v[1]+=c;
dfs1(j,k);
}
}
void dfs2(int k,int pa,int now){
for(auto [j,c]:adj[k]){
if(j==pa)v[k]=now+c;
}
for(auto [j,c]:adj[k]){
if(j==pa)continue;
dfs2(j,k,v[k]-c);
}
}
void dfs3(int k,int pa){
vector<int> t(1,k);
for(auto [j,c]:adj1[k]){
if(j==pa)continue;
deep[j]=deep[k]+c;
dfs3(j,k);
t.push_back(rec[j]);
}
sort(all(t),[&](int a,int b){return v[a]+deep[a]>v[b]+deep[b];});
rec[k]=t[0];
if(t.size()==1)return;
int cost=v[t[0]]+deep[t[0]]+v[t[1]]+deep[t[1]]-2*deep[k];
if(cost>best){
best=cost;
best_pair={t[0],t[1]};
}
}
int32_t main() {
LCBorz;
int n;cin>>n;
int total=0;
for(int i=1;i<n;i++){
int a,b,c,d;cin>>a>>b>>c>>d;
adj[a].push_back({b,d});
adj[b].push_back({a,c});
adj1[a].push_back({b,c+d});
adj1[b].push_back({a,c+d});
total+=c+d;
}
dfs1(1,1);
dfs2(1,1,v[1]);
dfs3(1,1);
best/=2;
int mx=*max_element(all(v));
cout<<total-best<<endl;
return 0;
}
Compilation message
designated_cities.cpp: In function 'int32_t main()':
designated_cities.cpp:64:9: warning: unused variable 'mx' [-Wunused-variable]
64 | int mx=*max_element(all(v));
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
268 ms |
41240 KB |
Output is correct |
3 |
Correct |
353 ms |
66956 KB |
Output is correct |
4 |
Correct |
253 ms |
39932 KB |
Output is correct |
5 |
Correct |
258 ms |
42292 KB |
Output is correct |
6 |
Correct |
279 ms |
44968 KB |
Output is correct |
7 |
Correct |
188 ms |
43556 KB |
Output is correct |
8 |
Correct |
310 ms |
55596 KB |
Output is correct |
9 |
Correct |
157 ms |
44836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |