답안 #465143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
465143 2021-08-15T08:51:55 Z nathanlee726 Designated Cities (JOI19_designated_cities) C++14
100 / 100
516 ms 54472 KB
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
 
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
 
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod; 
}
int GCD(int a,int b){
	int x=0;
	int ra,rb;
	while(a&&b){
		if(((a&1)==0)&&((b&1)==0)){
			a>>=1,b>>=1,x++;
		}
		else if((a^b)&1){
			if(a&1)b>>=1;
			else a>>=1;
		}
		else{
			ra=abs(a-b),rb=min(a,b);
			a=ra,b=rb;
		}
	}
	return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
vector<pair<int,pii> > g[200010];
vector<int> v;
int n,p,s=0,fa[200010],o[200010],vl[200010],mv[200010],qr[200010],so[200010],in[200010],val[200010],mvl[200010];
pii vv[200010],rt;
bool rd[200010];
void dfs(int p,int f){
	fa[p]=f;
	for(pair<int,pii> pi:g[p]){
		if(pi.X==f)continue;
		dfs(pi.X,p);
	}
}
int dfs1(int p,int f){
	int re=-1;
	for(pair<int,pii> pi:g[p]){
		if(pi.X==f)continue;
		int tr=dfs1(pi.X,p)+pi.Y.X;
		if(tr<0)continue;
		if(re==-1){
			re=tr;
		}
		else{
			if(tr>re)swap(tr,re);
			v.pb(tr);
		}
	}
	if(rd[p]){
		if(re>=0)v.pb(re);
		return -1e16;
	}
	else{
		if(re<0)return 0;
		return re;
	}
}
void dfs2(int p,int f){
	for(pair<int,pii> pi:g[p]){
		if(pi.X==f)continue;
		dfs2(pi.X,p);
		vl[p]+=(vl[pi.X]+pi.Y.Y);
		mv[pi.X]=vl[pi.X]+pi.Y.Y;
	}
}
int dfs3(int p,int f){
	int re=vl[p];
	for(pair<int,pii> pi:g[p]){
		if(pi.X==f)continue;
		vl[pi.X]+=vl[p]-mv[pi.X]+pi.Y.X;
		re=max(re,dfs3(pi.X,p)); 
	}
	return re;
}
pii dfs4(int p,int f){
	pii tp={-1,-1},tpp={-1,-1};
	for(pair<int,pii> pi:g[p]){
		if(pi.X==f)continue;
		pii tr=dfs4(pi.X,p);
		tr.X+=pi.Y.X;
		if(tr.X>=tp.X){
			swap(tp.X,tp.Y);
			swap(tpp.X,tpp.Y);
			tp.X=tr.X;
			tpp.X=tr.Y;
		}		
		else if(tr.X>=tp.Y){
			tp.Y=tr.X;
			tpp.Y=tr.Y;
		}
	}
	if(tp.X==-1){
		return {0,p};
	}
	else if(tp.Y==-1){
		if(o[2]>vl[p]-tp.X){
			o[2]=vl[p]-tp.X;
			rt={p,tpp.X};
		}
		return {tp.X,tpp.X};
	}
	else{
		if(o[2]>vl[p]-tp.X-tp.Y){
			o[2]=vl[p]-tp.X-tp.Y;
			rt={tpp.X,tpp.Y};
			bug(p,vl[p],tp.X,tp.Y,tpp.X,tpp.Y);
		}
		return {tp.X,tpp.X};
	}
}
void solve1(){
	memres(vl);
	memres(mv);		
	dfs2(0,-1);
	int x=dfs3(0,-1);
	o[1]=s-x;
}
void solve2(){
	o[2]=1e16;
	F(n)vl[i]=s-vl[i];
	dfs4(0,-1);
}
signed main(){
	IOS();
	cin>>n;
	F(n-1){
		int x,y,a,b;
		cin>>x>>y>>a>>b;
		s+=(a+b);
		--x,--y;
		g[x].pb({y,{a,b}});
		g[y].pb({x,{b,a}});
		in[x]++,in[y]++;
	} 
	int q;
	cin>>q;
	F(q)cin>>qr[i];
	solve1();
	solve2();
	int p=rt.X,pp=rt.Y;
	bug(p,pp);
	dfs(p,-1);
	int tp=pp;
	while(tp!=-1){
		if(fa[tp]!=-1)so[fa[tp]]=tp;
		rd[tp]=1;
		tp=fa[tp];
	}
	dfs1(p,-1);
	int ct=0;
	F(n)if(sz(g[i])==1)ct++;
	sort(all(v));
	int ms=0;
	for(int i=ct-1;i>=2;i--){
		ms+=v[ct-1-i];
		bug(v[ct-1-i],sz(v),ct);
		o[i]=ms;
	}
	F(q){
		if(qr[i]>=ct)cout<<0<<endl;
		else cout<<o[qr[i]]<<endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8140 KB Output is correct
3 Correct 5 ms 8140 KB Output is correct
4 Correct 5 ms 8140 KB Output is correct
5 Correct 5 ms 8140 KB Output is correct
6 Correct 5 ms 8140 KB Output is correct
7 Correct 6 ms 8092 KB Output is correct
8 Correct 6 ms 8140 KB Output is correct
9 Correct 6 ms 8140 KB Output is correct
10 Correct 5 ms 8140 KB Output is correct
11 Correct 5 ms 8140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8092 KB Output is correct
2 Correct 422 ms 33416 KB Output is correct
3 Correct 465 ms 51264 KB Output is correct
4 Correct 440 ms 30784 KB Output is correct
5 Correct 390 ms 33372 KB Output is correct
6 Correct 419 ms 36428 KB Output is correct
7 Correct 341 ms 32824 KB Output is correct
8 Correct 516 ms 50716 KB Output is correct
9 Correct 249 ms 33884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8140 KB Output is correct
2 Correct 397 ms 33396 KB Output is correct
3 Correct 449 ms 51352 KB Output is correct
4 Correct 414 ms 30756 KB Output is correct
5 Correct 378 ms 33380 KB Output is correct
6 Correct 439 ms 37120 KB Output is correct
7 Correct 252 ms 33704 KB Output is correct
8 Correct 486 ms 45736 KB Output is correct
9 Correct 241 ms 33476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8140 KB Output is correct
3 Correct 5 ms 8140 KB Output is correct
4 Correct 5 ms 8140 KB Output is correct
5 Correct 5 ms 8140 KB Output is correct
6 Correct 5 ms 8140 KB Output is correct
7 Correct 6 ms 8092 KB Output is correct
8 Correct 6 ms 8140 KB Output is correct
9 Correct 6 ms 8140 KB Output is correct
10 Correct 5 ms 8140 KB Output is correct
11 Correct 5 ms 8140 KB Output is correct
12 Correct 5 ms 8088 KB Output is correct
13 Correct 7 ms 8396 KB Output is correct
14 Correct 7 ms 8652 KB Output is correct
15 Correct 6 ms 8396 KB Output is correct
16 Correct 7 ms 8396 KB Output is correct
17 Correct 9 ms 8396 KB Output is correct
18 Correct 7 ms 8488 KB Output is correct
19 Correct 9 ms 8408 KB Output is correct
20 Correct 7 ms 8464 KB Output is correct
21 Correct 8 ms 8396 KB Output is correct
22 Correct 8 ms 8420 KB Output is correct
23 Correct 7 ms 8464 KB Output is correct
24 Correct 7 ms 8396 KB Output is correct
25 Correct 7 ms 8620 KB Output is correct
26 Correct 7 ms 8436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8092 KB Output is correct
2 Correct 422 ms 33416 KB Output is correct
3 Correct 465 ms 51264 KB Output is correct
4 Correct 440 ms 30784 KB Output is correct
5 Correct 390 ms 33372 KB Output is correct
6 Correct 419 ms 36428 KB Output is correct
7 Correct 341 ms 32824 KB Output is correct
8 Correct 516 ms 50716 KB Output is correct
9 Correct 249 ms 33884 KB Output is correct
10 Correct 6 ms 8140 KB Output is correct
11 Correct 397 ms 33396 KB Output is correct
12 Correct 449 ms 51352 KB Output is correct
13 Correct 414 ms 30756 KB Output is correct
14 Correct 378 ms 33380 KB Output is correct
15 Correct 439 ms 37120 KB Output is correct
16 Correct 252 ms 33704 KB Output is correct
17 Correct 486 ms 45736 KB Output is correct
18 Correct 241 ms 33476 KB Output is correct
19 Correct 5 ms 8140 KB Output is correct
20 Correct 399 ms 33472 KB Output is correct
21 Correct 442 ms 51280 KB Output is correct
22 Correct 399 ms 30780 KB Output is correct
23 Correct 416 ms 33924 KB Output is correct
24 Correct 381 ms 31232 KB Output is correct
25 Correct 394 ms 33844 KB Output is correct
26 Correct 392 ms 31308 KB Output is correct
27 Correct 375 ms 32872 KB Output is correct
28 Correct 403 ms 36700 KB Output is correct
29 Correct 420 ms 34376 KB Output is correct
30 Correct 341 ms 32120 KB Output is correct
31 Correct 269 ms 33820 KB Output is correct
32 Correct 425 ms 46208 KB Output is correct
33 Correct 218 ms 34036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8140 KB Output is correct
3 Correct 5 ms 8140 KB Output is correct
4 Correct 5 ms 8140 KB Output is correct
5 Correct 5 ms 8140 KB Output is correct
6 Correct 5 ms 8140 KB Output is correct
7 Correct 6 ms 8092 KB Output is correct
8 Correct 6 ms 8140 KB Output is correct
9 Correct 6 ms 8140 KB Output is correct
10 Correct 5 ms 8140 KB Output is correct
11 Correct 5 ms 8140 KB Output is correct
12 Correct 5 ms 8092 KB Output is correct
13 Correct 422 ms 33416 KB Output is correct
14 Correct 465 ms 51264 KB Output is correct
15 Correct 440 ms 30784 KB Output is correct
16 Correct 390 ms 33372 KB Output is correct
17 Correct 419 ms 36428 KB Output is correct
18 Correct 341 ms 32824 KB Output is correct
19 Correct 516 ms 50716 KB Output is correct
20 Correct 249 ms 33884 KB Output is correct
21 Correct 6 ms 8140 KB Output is correct
22 Correct 397 ms 33396 KB Output is correct
23 Correct 449 ms 51352 KB Output is correct
24 Correct 414 ms 30756 KB Output is correct
25 Correct 378 ms 33380 KB Output is correct
26 Correct 439 ms 37120 KB Output is correct
27 Correct 252 ms 33704 KB Output is correct
28 Correct 486 ms 45736 KB Output is correct
29 Correct 241 ms 33476 KB Output is correct
30 Correct 5 ms 8088 KB Output is correct
31 Correct 7 ms 8396 KB Output is correct
32 Correct 7 ms 8652 KB Output is correct
33 Correct 6 ms 8396 KB Output is correct
34 Correct 7 ms 8396 KB Output is correct
35 Correct 9 ms 8396 KB Output is correct
36 Correct 7 ms 8488 KB Output is correct
37 Correct 9 ms 8408 KB Output is correct
38 Correct 7 ms 8464 KB Output is correct
39 Correct 8 ms 8396 KB Output is correct
40 Correct 8 ms 8420 KB Output is correct
41 Correct 7 ms 8464 KB Output is correct
42 Correct 7 ms 8396 KB Output is correct
43 Correct 7 ms 8620 KB Output is correct
44 Correct 7 ms 8436 KB Output is correct
45 Correct 5 ms 8140 KB Output is correct
46 Correct 399 ms 33472 KB Output is correct
47 Correct 442 ms 51280 KB Output is correct
48 Correct 399 ms 30780 KB Output is correct
49 Correct 416 ms 33924 KB Output is correct
50 Correct 381 ms 31232 KB Output is correct
51 Correct 394 ms 33844 KB Output is correct
52 Correct 392 ms 31308 KB Output is correct
53 Correct 375 ms 32872 KB Output is correct
54 Correct 403 ms 36700 KB Output is correct
55 Correct 420 ms 34376 KB Output is correct
56 Correct 341 ms 32120 KB Output is correct
57 Correct 269 ms 33820 KB Output is correct
58 Correct 425 ms 46208 KB Output is correct
59 Correct 218 ms 34036 KB Output is correct
60 Correct 5 ms 8140 KB Output is correct
61 Correct 420 ms 37664 KB Output is correct
62 Correct 488 ms 54472 KB Output is correct
63 Correct 415 ms 35072 KB Output is correct
64 Correct 421 ms 37880 KB Output is correct
65 Correct 435 ms 35300 KB Output is correct
66 Correct 407 ms 38128 KB Output is correct
67 Correct 453 ms 35252 KB Output is correct
68 Correct 419 ms 37288 KB Output is correct
69 Correct 472 ms 40768 KB Output is correct
70 Correct 436 ms 38476 KB Output is correct
71 Correct 386 ms 36344 KB Output is correct
72 Correct 324 ms 38600 KB Output is correct
73 Correct 494 ms 50012 KB Output is correct
74 Correct 262 ms 39624 KB Output is correct