답안 #525403

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525403 2022-02-11T14:31:12 Z Koosha_mv Designated Cities (JOI19_designated_cities) C++14
100 / 100
490 ms 104004 KB
#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,cnt,to[N],ts[N],w[N],res[N],ans[N],pes[N],h[N],mark[N],par[N],st[N],ft[N],wp[N],lazy[N];
pair<int,int> dp[N],seg[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;
		Y=vec[2].S;
	}
	minm(ans[1],res[u]);
}
void dfs3(int u,int p){
	ts[cnt]=u;
	st[u]=cnt++;
	par[u]=p;
	for(auto id : g[u]){
		int v=to[id];
		if(v==p) continue ;
		h[v]=h[u]+w[id];
		wp[v]=w[id];
		sum+=w[id];
		dfs3(v,u);
	}	
	ft[u]=cnt;
}
void shift(int id){
	seg[id<<1].F+=lazy[id];
	seg[id<<1|1].F+=lazy[id];
	lazy[id<<1]+=lazy[id];
	lazy[id<<1|1]+=lazy[id];
	lazy[id]=0;
}
void build(int id=1,int l=0,int r=n){
	if(l+1==r){
		seg[id]={h[ts[l]],ts[l]};
		return ;
	}
	int mid=(l+r)>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid,r);
	seg[id]=max(seg[id<<1],seg[id<<1|1]);
}
void add(int L,int R,int val,int id=1,int l=0,int r=n){
	if(r<=L || R<=l) return ;
	if(L<=l && r<=R){
		seg[id].F+=val;
		lazy[id]+=val;
		return ;
	}
	int mid=(l+r)>>1;
	shift(id);
	add(L,R,val,id<<1,l,mid);
	add(L,R,val,id<<1|1,mid,r);
	seg[id]=max(seg[id<<1],seg[id<<1|1]);
}
void find(){
	fill(ans,ans+N,inf);
	rt=1;
	while(g[rt].size()==1) rt++;
	dfs1(rt,0);
	dfs2(rt,0);
	rt=X;
	dfs3(rt,0);
	mark[rt]=1;
}
void do_it(){
	build();	
	f(i,2,n+1){
		int u=seg[1].S;
		while(!mark[u]){
			mark[u]=1;
			sum-=wp[u];
			add(st[u],ft[u],-wp[u]);
			u=par[u];
		}
		ans[i]=sum;
	}
}

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();
	do_it();
	
	cin>>q;
	while(q--){
		int x;
		cin>>x;
		cout<<ans[x]<<'\n';
	}
}

Compilation message

designated_cities.cpp:131:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  131 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 16 ms 31720 KB Output is correct
3 Correct 16 ms 31692 KB Output is correct
4 Correct 16 ms 31700 KB Output is correct
5 Correct 16 ms 31688 KB Output is correct
6 Correct 18 ms 31692 KB Output is correct
7 Correct 17 ms 31652 KB Output is correct
8 Correct 17 ms 31692 KB Output is correct
9 Correct 17 ms 31692 KB Output is correct
10 Correct 16 ms 31708 KB Output is correct
11 Correct 17 ms 31616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23804 KB Output is correct
2 Correct 413 ms 75148 KB Output is correct
3 Correct 478 ms 99140 KB Output is correct
4 Correct 386 ms 74980 KB Output is correct
5 Correct 380 ms 74808 KB Output is correct
6 Correct 422 ms 78312 KB Output is correct
7 Correct 341 ms 74804 KB Output is correct
8 Correct 490 ms 100072 KB Output is correct
9 Correct 273 ms 75296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 415 ms 74800 KB Output is correct
3 Correct 462 ms 103420 KB Output is correct
4 Correct 397 ms 74840 KB Output is correct
5 Correct 436 ms 74812 KB Output is correct
6 Correct 434 ms 78864 KB Output is correct
7 Correct 291 ms 75196 KB Output is correct
8 Correct 470 ms 90600 KB Output is correct
9 Correct 274 ms 75312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 16 ms 31720 KB Output is correct
3 Correct 16 ms 31692 KB Output is correct
4 Correct 16 ms 31700 KB Output is correct
5 Correct 16 ms 31688 KB Output is correct
6 Correct 18 ms 31692 KB Output is correct
7 Correct 17 ms 31652 KB Output is correct
8 Correct 17 ms 31692 KB Output is correct
9 Correct 17 ms 31692 KB Output is correct
10 Correct 16 ms 31708 KB Output is correct
11 Correct 17 ms 31616 KB Output is correct
12 Correct 17 ms 23828 KB Output is correct
13 Correct 19 ms 32136 KB Output is correct
14 Correct 17 ms 32332 KB Output is correct
15 Correct 17 ms 32068 KB Output is correct
16 Correct 19 ms 32076 KB Output is correct
17 Correct 19 ms 32132 KB Output is correct
18 Correct 18 ms 32068 KB Output is correct
19 Correct 17 ms 32168 KB Output is correct
20 Correct 18 ms 32140 KB Output is correct
21 Correct 19 ms 32076 KB Output is correct
22 Correct 19 ms 32076 KB Output is correct
23 Correct 17 ms 32168 KB Output is correct
24 Correct 19 ms 32188 KB Output is correct
25 Correct 18 ms 32332 KB Output is correct
26 Correct 18 ms 32152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23804 KB Output is correct
2 Correct 413 ms 75148 KB Output is correct
3 Correct 478 ms 99140 KB Output is correct
4 Correct 386 ms 74980 KB Output is correct
5 Correct 380 ms 74808 KB Output is correct
6 Correct 422 ms 78312 KB Output is correct
7 Correct 341 ms 74804 KB Output is correct
8 Correct 490 ms 100072 KB Output is correct
9 Correct 273 ms 75296 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 415 ms 74800 KB Output is correct
12 Correct 462 ms 103420 KB Output is correct
13 Correct 397 ms 74840 KB Output is correct
14 Correct 436 ms 74812 KB Output is correct
15 Correct 434 ms 78864 KB Output is correct
16 Correct 291 ms 75196 KB Output is correct
17 Correct 470 ms 90600 KB Output is correct
18 Correct 274 ms 75312 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 398 ms 74824 KB Output is correct
21 Correct 460 ms 104004 KB Output is correct
22 Correct 373 ms 74832 KB Output is correct
23 Correct 406 ms 75112 KB Output is correct
24 Correct 393 ms 74812 KB Output is correct
25 Correct 429 ms 74860 KB Output is correct
26 Correct 379 ms 74788 KB Output is correct
27 Correct 382 ms 74752 KB Output is correct
28 Correct 438 ms 77652 KB Output is correct
29 Correct 408 ms 75064 KB Output is correct
30 Correct 364 ms 74884 KB Output is correct
31 Correct 332 ms 74936 KB Output is correct
32 Correct 467 ms 91876 KB Output is correct
33 Correct 272 ms 75256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 16 ms 31720 KB Output is correct
3 Correct 16 ms 31692 KB Output is correct
4 Correct 16 ms 31700 KB Output is correct
5 Correct 16 ms 31688 KB Output is correct
6 Correct 18 ms 31692 KB Output is correct
7 Correct 17 ms 31652 KB Output is correct
8 Correct 17 ms 31692 KB Output is correct
9 Correct 17 ms 31692 KB Output is correct
10 Correct 16 ms 31708 KB Output is correct
11 Correct 17 ms 31616 KB Output is correct
12 Correct 12 ms 23804 KB Output is correct
13 Correct 413 ms 75148 KB Output is correct
14 Correct 478 ms 99140 KB Output is correct
15 Correct 386 ms 74980 KB Output is correct
16 Correct 380 ms 74808 KB Output is correct
17 Correct 422 ms 78312 KB Output is correct
18 Correct 341 ms 74804 KB Output is correct
19 Correct 490 ms 100072 KB Output is correct
20 Correct 273 ms 75296 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 415 ms 74800 KB Output is correct
23 Correct 462 ms 103420 KB Output is correct
24 Correct 397 ms 74840 KB Output is correct
25 Correct 436 ms 74812 KB Output is correct
26 Correct 434 ms 78864 KB Output is correct
27 Correct 291 ms 75196 KB Output is correct
28 Correct 470 ms 90600 KB Output is correct
29 Correct 274 ms 75312 KB Output is correct
30 Correct 17 ms 23828 KB Output is correct
31 Correct 19 ms 32136 KB Output is correct
32 Correct 17 ms 32332 KB Output is correct
33 Correct 17 ms 32068 KB Output is correct
34 Correct 19 ms 32076 KB Output is correct
35 Correct 19 ms 32132 KB Output is correct
36 Correct 18 ms 32068 KB Output is correct
37 Correct 17 ms 32168 KB Output is correct
38 Correct 18 ms 32140 KB Output is correct
39 Correct 19 ms 32076 KB Output is correct
40 Correct 19 ms 32076 KB Output is correct
41 Correct 17 ms 32168 KB Output is correct
42 Correct 19 ms 32188 KB Output is correct
43 Correct 18 ms 32332 KB Output is correct
44 Correct 18 ms 32152 KB Output is correct
45 Correct 13 ms 23756 KB Output is correct
46 Correct 398 ms 74824 KB Output is correct
47 Correct 460 ms 104004 KB Output is correct
48 Correct 373 ms 74832 KB Output is correct
49 Correct 406 ms 75112 KB Output is correct
50 Correct 393 ms 74812 KB Output is correct
51 Correct 429 ms 74860 KB Output is correct
52 Correct 379 ms 74788 KB Output is correct
53 Correct 382 ms 74752 KB Output is correct
54 Correct 438 ms 77652 KB Output is correct
55 Correct 408 ms 75064 KB Output is correct
56 Correct 364 ms 74884 KB Output is correct
57 Correct 332 ms 74936 KB Output is correct
58 Correct 467 ms 91876 KB Output is correct
59 Correct 272 ms 75256 KB Output is correct
60 Correct 13 ms 23840 KB Output is correct
61 Correct 457 ms 76280 KB Output is correct
62 Correct 485 ms 99832 KB Output is correct
63 Correct 410 ms 76256 KB Output is correct
64 Correct 444 ms 76560 KB Output is correct
65 Correct 423 ms 76064 KB Output is correct
66 Correct 434 ms 76228 KB Output is correct
67 Correct 439 ms 76100 KB Output is correct
68 Correct 413 ms 76320 KB Output is correct
69 Correct 455 ms 79212 KB Output is correct
70 Correct 444 ms 76356 KB Output is correct
71 Correct 397 ms 76492 KB Output is correct
72 Correct 376 ms 77116 KB Output is correct
73 Correct 488 ms 90276 KB Output is correct
74 Correct 314 ms 78252 KB Output is correct