This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma gcc diagnostic "-std=c++1z"
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
using namespace std;
const int N=2e5+5;
int n,m,T,k,q,tot,sum,val[N];
int ans=1e18;
pii mx={0,0};
vector<pair<int,pii>> g[N];
void dfs0(int x, int p) {
for (auto A:g[x]) {
int y=A.f,w1=A.s.f,w2=A.s.s;
if (y==p) continue;
sum+=w2; dfs0(y,x);
}
}
void dfs1(int x, int p) {
val[x]=sum;
for (auto A:g[x]) {
int y=A.f,w1=A.s.f,w2=A.s.s;
if (y==p) continue;
sum+=w1-w2; dfs1(y,x); sum-=w1-w2;
}
}
void dfs(int x, int p, int dist) {
mx=max(mx,{dist+val[x],x});
for (auto A:g[x]) {
int y=A.f,w1=A.s.f,w2=A.s.s;
if (y==p) continue;
dfs(y,x,dist+w1+w2);
}
}
void test_case() {
cin>>n;
for (int i=1; i<=n-1; i++) {
int a,b,c,d; cin>>a>>b>>c>>d;
g[a].pb({b,{c,d}}); g[b].pb({a,{d,c}});
tot+=c+d;
}
cin>>q;
int query[q+1];
for (int i=1; i<=q; i++) cin>>query[i];
dfs0(1,0); dfs1(1,1);
dfs(1,0,val[1]); int r1=mx.s; dfs(r1,0,val[r1]); int r2=mx.s;
cout<<tot-mx.f/2<<endl;
}
main () {
ios :: sync_with_stdio(0);
cin.tie(0); cout.tie(0);
T=1;
while (T--) test_case();
}
Compilation message (stderr)
designated_cities.cpp:1: warning: ignoring '#pragma gcc diagnostic' [-Wunknown-pragmas]
1 | #pragma gcc diagnostic "-std=c++1z"
|
designated_cities.cpp: In function 'void dfs0(long long int, long long int)':
designated_cities.cpp:21:21: warning: unused variable 'w1' [-Wunused-variable]
21 | int y=A.f,w1=A.s.f,w2=A.s.s;
| ^~
designated_cities.cpp: In function 'void test_case()':
designated_cities.cpp:56:59: warning: unused variable 'r2' [-Wunused-variable]
56 | dfs(1,0,val[1]); int r1=mx.s; dfs(r1,0,val[r1]); int r2=mx.s;
| ^~
designated_cities.cpp: At global scope:
designated_cities.cpp:60:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
60 | main () {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |