#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1e6+99,inf=1e15;
int n,t,rt,X,Y,q,sum,mv,to[N],w[N],res[N],ans[N],pes[N],h[N],mark[N],par[N];
pair<int,int> dp[N];
vector<int> g[N];
void dfs1(int u,int p){
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
res[rt]+=w[id];
dfs1(v,u);
}
}
void dfs2(int u,int p){
vector<pair<int,int> > vec(3);
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
res[v]=res[u]-w[id]+w[id^1];
dfs2(v,u);
dp[v].F+=w[id];
vec[0]=dp[v];
sort(all(vec));
}
if(vec[2].S==0){
dp[u]={0,u};
}
else{
dp[u]=vec[2];
}
pes[u]=inf;
if(vec[1].S!=0 && res[u]-vec[1].F-vec[2].F<ans[2]){
ans[2]=res[u]-vec[1].F-vec[2].F;
X=vec[1].S;
if(!X) assert(0);
Y=vec[2].S;
mv=1;
}
minm(ans[1],res[u]);
}
void dfs3(int u,int p){
par[u]=p;
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
dfs3(v,u);
sum+=w[id];
}
}
void dfs4(int u,int p){
//cout<<u<<" "<<h[u]<<endl;
if(h[u]>h[X]){
X=u;
}
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
if(!mark[v]){
h[v]=h[u]+w[id];
}
else{
h[v]=0;
}
dfs4(v,u);
}
}
void find(){
fill(ans,ans+N,inf);
rt=1;
dfs1(rt,0);
dfs2(rt,0);
//if(!X) assert(0);
rt=Y;
dfs3(rt,0);
//cout<<rt<<" "<<sum<<endl;
f(i,2,n+1){
X=rt;
dfs4(rt,0);
sum-=h[X];
ans[i]=sum;
while(X){
mark[X]=1;
X=par[X];
}
}
}
main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
cin>>n;
f(i,1,n){
int u,v;
cin>>u>>v>>w[2*i]>>w[2*i+1];
to[2*i]=v;
to[2*i+1]=u;
g[u].pb(2*i);
g[v].pb(2*i+1);
}
if(n==2){
ans[1]=min(w[2],w[3]);
cin>>q;
while(q--){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
exit(0);
}
find();
cin>>q;
while(q--){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
}
Compilation message
designated_cities.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
112 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
19 ms |
31676 KB |
Output is correct |
3 |
Correct |
17 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31560 KB |
Output is correct |
5 |
Correct |
20 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31612 KB |
Output is correct |
8 |
Correct |
16 ms |
31564 KB |
Output is correct |
9 |
Correct |
16 ms |
31572 KB |
Output is correct |
10 |
Correct |
17 ms |
31564 KB |
Output is correct |
11 |
Correct |
16 ms |
31652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Execution timed out |
2085 ms |
56188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Execution timed out |
2056 ms |
56328 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
19 ms |
31676 KB |
Output is correct |
3 |
Correct |
17 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31560 KB |
Output is correct |
5 |
Correct |
20 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31612 KB |
Output is correct |
8 |
Correct |
16 ms |
31564 KB |
Output is correct |
9 |
Correct |
16 ms |
31572 KB |
Output is correct |
10 |
Correct |
17 ms |
31564 KB |
Output is correct |
11 |
Correct |
16 ms |
31652 KB |
Output is correct |
12 |
Correct |
12 ms |
23756 KB |
Output is correct |
13 |
Correct |
106 ms |
31920 KB |
Output is correct |
14 |
Correct |
110 ms |
32092 KB |
Output is correct |
15 |
Correct |
97 ms |
31928 KB |
Output is correct |
16 |
Correct |
98 ms |
31940 KB |
Output is correct |
17 |
Correct |
100 ms |
31944 KB |
Output is correct |
18 |
Correct |
99 ms |
31944 KB |
Output is correct |
19 |
Correct |
99 ms |
31828 KB |
Output is correct |
20 |
Correct |
95 ms |
31940 KB |
Output is correct |
21 |
Correct |
99 ms |
31948 KB |
Output is correct |
22 |
Correct |
99 ms |
31936 KB |
Output is correct |
23 |
Correct |
94 ms |
32020 KB |
Output is correct |
24 |
Correct |
61 ms |
31928 KB |
Output is correct |
25 |
Correct |
110 ms |
32172 KB |
Output is correct |
26 |
Correct |
58 ms |
31940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Execution timed out |
2085 ms |
56188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
19 ms |
31676 KB |
Output is correct |
3 |
Correct |
17 ms |
31564 KB |
Output is correct |
4 |
Correct |
16 ms |
31560 KB |
Output is correct |
5 |
Correct |
20 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31612 KB |
Output is correct |
8 |
Correct |
16 ms |
31564 KB |
Output is correct |
9 |
Correct |
16 ms |
31572 KB |
Output is correct |
10 |
Correct |
17 ms |
31564 KB |
Output is correct |
11 |
Correct |
16 ms |
31652 KB |
Output is correct |
12 |
Correct |
13 ms |
23756 KB |
Output is correct |
13 |
Execution timed out |
2085 ms |
56188 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |