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>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=300010, LOG=20;
int n, m, k, u, v, x, y, t, a, b;
int deg[MAXN];
bool dead[MAXN];
ll ans[MAXN], sum;
vector<piii> G[MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;
piii getpar(int v){
for (auto p:G[v]) if (!dead[p.first]) return p;
debug("shit")
cout<<1/0;
}
void dfs1(int node, int par){
for (auto p:G[node]) if (p.first!=par){
dfs1(p.first, node);
sum+=p.second.first;
}
}
void dfs2(int node, int par){
ans[1]=min(ans[1], sum);
for (auto p:G[node]) if (p.first!=par){
sum-=p.second.first;
sum+=p.second.second;
dfs2(p.first, node);
sum+=p.second.first;
sum-=p.second.second;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n;
for (int i=1; i<n; i++){
cin>>u>>v>>x>>y;
G[u].pb({v, {x, y}});
G[v].pb({u, {y, x}});
deg[u]++;
deg[v]++;
}
for (int i=1; i<=n; i++) if (deg[i]==1){
piii p=getpar(i);
// debug2(i, p.second.second)
pq.push({p.second.second, i});
}
ans[1]=INF;
dfs1(1, 0);
// debug(sum)
dfs2(1, 0);
sum=0;
while (pq.size()>2){
pll p=pq.top();
pq.pop();
int v=p.second;
dead[v]=1;
piii pp=getpar(v);
int u=pp.first;
// debug2(v, u)
// debug(p.first)
// cerr<<"\n";
deg[u]--;
if (deg[u]>1){
sum+=p.first;
ans[pq.size()]=sum;
continue ;
}
pp=getpar(u);
pq.push({p.first+pp.second.second, u});
}
cin>>m;
while (m--){
cin>>x;
cout<<ans[x]<<"\n";
}
return 0;
}
Compilation message (stderr)
designated_cities.cpp: In function 'piii getpar(int)':
designated_cities.cpp:35:9: warning: division by zero [-Wdiv-by-zero]
35 | cout<<1/0;
| ~^~
designated_cities.cpp:35:10: warning: control reaches end of non-void function [-Wreturn-type]
35 | cout<<1/0;
| ^
# | 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... |