This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 2e5+5;
ll N, Q, i, ans[MN], tot, cur, x, y, a, b, dist[MN], w[2*MN], par[MN];
vector<pair<ll,ll>> adj[MN], ord;
struct pq{bool operator()(const pair<ll,ll>&i,const pair<ll,ll>&j){return i.second>j.second;}};
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,pq> q;
ll meh(ll n,ll p){
ll cur = 0;
for(auto v : adj[n]){
if(v.first==p) continue;
cur+=w[v.second^1]+meh(v.first,n);
}
return cur;
}
void dfs(ll n,ll p){
ans[1]=max(ans[1],cur);
for(auto v : adj[n]){
if(v.first==p) continue;
cur += w[v.second]-w[v.second^1]; dfs(v.first,n);
cur -= w[v.second]-w[v.second^1];
}
}
pair<ll,ll> dfs2(ll n,ll p){
pair<ll,ll> tmp(n,0);
for(auto v : adj[n]){
if(v.first==p) continue;
auto tt = dfs2(v.first, n);
tt.second += w[v.second];
if(tt.second<tmp.second) ord.push_back(tt);
else{swap(tmp,tt); ord.push_back(tt);}
}
return tmp;
}
int main(){
memset(ans,-1,sizeof(ans));
for(scanf("%lld",&N),i=1;i<N;i++){
scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
adj[x].push_back({y,2*i-2});
adj[y].push_back({x,2*i-1});
w[2*i-2]=a; w[2*i-1]=b;
tot += a+b;
}
cur=meh(1, 0); dfs(1, 0); ans[1]=tot-ans[1];
memset(dist,-1,sizeof(dist));
q.push({1, 0});
while(q.size()){
auto t = q.top(); q.pop();
if(dist[t.first]!=-1) continue;
dist[t.first]=t.second; b = t.first;
for(auto v : adj[t.first]){
q.push({v.first,w[v.second]+t.second});
}
}
memset(dist,-1,sizeof(dist));
memset(par,-1,sizeof(par));
q.push({b,0}); par[b]=0;
while(q.size()){
auto t = q.top(); q.pop();
if(dist[t.first]!=-1) continue;
dist[t.first]=t.second; a = t.first;
for(auto v : adj[t.first]){
q.push({v.first,w[v.second]+t.second});
if(par[v.first]==-1) par[v.first]=t.first;
}
}
ll lst = -1; cur = 0;
while(a){
for(auto v : adj[a]){
if(v.first==lst||v.first==par[a])
cur += w[v.second^1];
else{
cur += meh(v.first, a)+w[v.second^1];
auto tmp = dfs2(v.first, a);
tmp.second += w[v.second];
ord.push_back(tmp);
}
}
lst = a; a = par[a];
}
sort(ord.begin(),ord.end(),[](pair<ll,ll>i,pair<ll,ll>j){return i.second>j.second;});
ans[2] = tot - cur;
for(i=0;i<ord.size();i++){
ans[3+i]=ans[2+i]-ord[i].second;
}
for(i=1;i<=N;i++){if(ans[i]==-1) ans[i]=ans[i-1];}
for(scanf("%lld",&Q);Q;Q--){
scanf("%lld",&x);
printf("%lld\n",ans[x]);
}
return 0;
}
Compilation message (stderr)
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:85:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<ord.size();i++){
~^~~~~~~~~~~
designated_cities.cpp:39:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%lld",&N),i=1;i<N;i++){
~~~~~~~~~~~~~~~~^~~~
designated_cities.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%lld",&Q);Q;Q--){
~~~~~^~~~~~~~~~~
designated_cities.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&x);
~~~~~^~~~~~~~~~~
# | 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... |