답안 #282326

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
282326 2020-08-24T09:57:54 Z shrek12357 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1142 ms 248824 KB
#include <bits/stdc++.h>
 
#define pb push_back
#define F first
#define S second
 
using namespace std;
 
typedef long long ll;
typedef string str;
 
const ll Mod = 1e9 + 7;
const int Maxn = 1e6 + 100;
 
vector<ll> G[Maxn];
ll dp[Maxn], son[Maxn], m1[Maxn], m2[Maxn];
ll t, m;
vector<ll> V;
vector<ll> S[Maxn];
bool DFS(ll u, ll p){
	bool res = (u == m), r2;
	ll cnt = 0;
	ll mx = 0;
	ll mx2 = 0;
	
	for(auto adj : G[u]){
		if(adj == p) continue;
		r2 =DFS(adj, u);
		res |= r2;
		if(!r2){
			S[u].pb(adj);
			cnt ++;
			if(dp[adj] > mx){
				mx2 = mx;
				mx = dp[adj];
			} else mx2 = max(mx2, dp[adj]);
		}
	}
	dp[u] = cnt + mx2;
	m2[u] = mx2;
	m1[u] = mx;
	son[u] = cnt;
	if(res){
		V.pb(u);
	}
	return res;
}
ll dp2[Maxn];
vector< pair<ll, ll> > A;
ll C[Maxn];
 
bool CMP(ll i, ll j){
	return dp[i] > dp[j];
}
ll ps[Maxn];
 
bool check(ll x){
	ll M = V.size();
	ll c = 0;
	for(int i = 0; i < M - 1; i++){
		ll SS = S[V[i]].size();
		for(int j = 0; j < SS; j++){
			if(c + dp[S[V[i]][j]] + (SS - j) + ps[i] > x) {
				c++;
			} else break;
		}
		//cerr << c << '\n';
		if(c > i + 1) return false;
	}
	
	//cerr << "!!!" << x << '\n';
	return (c <= x);
}
 
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n >> t >> m;
	ll u, v;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	DFS(t, -1);
	
	
	ll sz = V.size();
	dp2[sz-1] = 0;
	ll sm = 0;
	ps[sz-1] = 0;
	for(int i = sz - 2; i >= 0; i--){
		ps[i] = sm;
		sm += son[V[i]];
		
		//cerr << son[V[i]] << '\n';
		ll K = 0;
		sort(S[V[i]].begin(), S[V[i]].end(), CMP);
		for(auto ch : S[V[i]]){
			//cerr << '!' << ch << ' ' << dp[ch] << ' ' << sm << '\n';
			A.pb({dp[ch] + sm - K, i + 1});
			K ++;
		}
		
		dp2[i] = min( max(dp2[i + 1], sm-son[V[i]] + m1[V[i]] + son[V[i]]), 1LL + max(dp2[i + 1], sm-son[V[i]] + m2[V[i]] + son[V[i]] - 1) );
		
	}
	sort(A.begin(), A.end()); reverse(A.begin(), A.end());
	ll M = A.size();
	ll cnt = 1;
	//cerr << '\n';
	//dp2[0] = 100000000;
	/*
	ll ans = min(dp2[0], (M > 0 ? A[0].F : 0));
	bool fl=true;;
	A.pb({0, 0});
	for(int i = 0; i < M; i++){
		for(int j = A[i].S; j <= n; j++){
			C[j] ++;
			if(C[j] > j) fl = false;
		}
		//cerr << i << " " << fl << '\n';
		if(fl) ans = min(ans, i + 1 + A[i + 1].F);
		cerr << i + 1 + A[i + 1].F << '\n';
	}
	cout << ans << '\n';
	*/
	//cout << dp2[0] << '\n';
	ll l = -1, r = n, mid;
	while(l + 1 < r){
		mid = (l + r) >> 1;
		if(check(mid)) r = mid;
		else l = mid;
	} 
	//cerr << check(1) << '\n';
	cout << r;
	return 0;
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:109:5: warning: unused variable 'M' [-Wunused-variable]
  109 |  ll M = A.size();
      |     ^
mousetrap.cpp:110:5: warning: unused variable 'cnt' [-Wunused-variable]
  110 |  ll cnt = 1;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47352 KB Output is correct
2 Correct 32 ms 47480 KB Output is correct
3 Correct 32 ms 47352 KB Output is correct
4 Correct 31 ms 47356 KB Output is correct
5 Correct 33 ms 47360 KB Output is correct
6 Correct 32 ms 47352 KB Output is correct
7 Correct 33 ms 47360 KB Output is correct
8 Correct 32 ms 47352 KB Output is correct
9 Correct 32 ms 47352 KB Output is correct
10 Correct 32 ms 47352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 448 ms 147032 KB Output is correct
2 Correct 402 ms 136952 KB Output is correct
3 Correct 1105 ms 148064 KB Output is correct
4 Correct 534 ms 97528 KB Output is correct
5 Correct 1110 ms 147960 KB Output is correct
6 Correct 1135 ms 148028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47352 KB Output is correct
2 Correct 32 ms 47480 KB Output is correct
3 Correct 32 ms 47352 KB Output is correct
4 Correct 31 ms 47356 KB Output is correct
5 Correct 33 ms 47360 KB Output is correct
6 Correct 32 ms 47352 KB Output is correct
7 Correct 33 ms 47360 KB Output is correct
8 Correct 32 ms 47352 KB Output is correct
9 Correct 32 ms 47352 KB Output is correct
10 Correct 32 ms 47352 KB Output is correct
11 Correct 32 ms 47372 KB Output is correct
12 Correct 32 ms 47480 KB Output is correct
13 Correct 34 ms 47480 KB Output is correct
14 Correct 32 ms 47488 KB Output is correct
15 Correct 32 ms 47460 KB Output is correct
16 Correct 33 ms 47488 KB Output is correct
17 Correct 33 ms 47488 KB Output is correct
18 Correct 32 ms 47488 KB Output is correct
19 Correct 34 ms 47488 KB Output is correct
20 Correct 33 ms 47480 KB Output is correct
21 Correct 37 ms 47488 KB Output is correct
22 Correct 33 ms 47480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47352 KB Output is correct
2 Correct 32 ms 47480 KB Output is correct
3 Correct 32 ms 47352 KB Output is correct
4 Correct 31 ms 47356 KB Output is correct
5 Correct 33 ms 47360 KB Output is correct
6 Correct 32 ms 47352 KB Output is correct
7 Correct 33 ms 47360 KB Output is correct
8 Correct 32 ms 47352 KB Output is correct
9 Correct 32 ms 47352 KB Output is correct
10 Correct 32 ms 47352 KB Output is correct
11 Correct 448 ms 147032 KB Output is correct
12 Correct 402 ms 136952 KB Output is correct
13 Correct 1105 ms 148064 KB Output is correct
14 Correct 534 ms 97528 KB Output is correct
15 Correct 1110 ms 147960 KB Output is correct
16 Correct 1135 ms 148028 KB Output is correct
17 Correct 32 ms 47372 KB Output is correct
18 Correct 32 ms 47480 KB Output is correct
19 Correct 34 ms 47480 KB Output is correct
20 Correct 32 ms 47488 KB Output is correct
21 Correct 32 ms 47460 KB Output is correct
22 Correct 33 ms 47488 KB Output is correct
23 Correct 33 ms 47488 KB Output is correct
24 Correct 32 ms 47488 KB Output is correct
25 Correct 34 ms 47488 KB Output is correct
26 Correct 33 ms 47480 KB Output is correct
27 Correct 37 ms 47488 KB Output is correct
28 Correct 33 ms 47480 KB Output is correct
29 Correct 32 ms 47360 KB Output is correct
30 Correct 444 ms 146996 KB Output is correct
31 Correct 441 ms 146808 KB Output is correct
32 Correct 485 ms 241240 KB Output is correct
33 Correct 463 ms 248824 KB Output is correct
34 Correct 1092 ms 148132 KB Output is correct
35 Correct 1093 ms 148216 KB Output is correct
36 Correct 1086 ms 162528 KB Output is correct
37 Correct 1142 ms 162536 KB Output is correct
38 Correct 917 ms 159448 KB Output is correct
39 Correct 897 ms 159484 KB Output is correct
40 Correct 857 ms 159460 KB Output is correct