답안 #563716

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563716 2022-05-18T03:44:18 Z ngpin04 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
765 ms 158568 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) {
	return l + rd() % (r - l + 1);
}
const int N = 1e6 + 5; 
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

vector <int> adj[N];
vector <int> a;

int deg[N];
int par[N];
int dp[N];
int n,t,s;

bool flag[N];

void dfs(int u, int p = -1) {
	par[u] = p;
	for (int v : adj[u]) 
		if (v != p) 
			dfs(v, u);
}

void solve(int u, int p = -1) {
	deg[u] = 0;
	pair <int, int> pir = mp(0, 0);
	for (int v : adj[u]) if (v != p) {
		solve(v, u);
		deg[u]++;
		if (maxi(pir.se, dp[v])) {
			if (pir.fi < pir.se)
				swap(pir.fi, pir.se);
		}
	}
	dp[u] = pir.se + deg[u];
}

bool check(int lim) {
	int tot = 0;
	for (int v : a)
		tot += deg[v];
	if (lim < tot)
		return false;
			
	for (int i = 0, cnt = 0; i < (int) a.size(); i++) {
		int u = a[i];
		int skipped = 0;
		for (int v : adj[u]) 
			if (!flag[v]) 
				skipped += (dp[v] + tot) <= lim;
				 	
		for (int v : adj[u]) 
			if (!flag[v])
				cnt += (dp[v] + tot > lim);
				
		tot -= skipped;
		
		if (cnt > i + 1)
			return false;
			
	}
	return true;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	#ifdef ONLINE_JUDGE
	freopen("mousetrap.inp","r",stdin);
	freopen("mousetrap.out","w",stdout);
	#endif
	cin >> n >> t >> s;
	for (int i = 1; i < n; i++) {
		int u,v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	dfs(s);
	
	while (t != -1) {
		a.push_back(t);
		t = par[t];
	}
	reverse(ALL(a));
	for (int v : a)
		flag[v] = true;
	a.pop_back();
	
		
	for (int u : a) {
		deg[u] = 0;
		for (int v : adj[u]) if (!flag[v]) {
			solve(v, u);
			deg[u]++;
			// cerr << u << " " << v << "\n";
		}
	}
	
	int lo = -1;
	int hi = n;
	while (hi - lo > 1) {
		int mid = (lo + hi) >> 1;
		if (check(mid))
			hi = mid;
		else
			lo = mid;
	}
	
	cout << hi;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23752 KB Output is correct
3 Correct 12 ms 23800 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23828 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 14 ms 23872 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 66816 KB Output is correct
2 Correct 240 ms 62552 KB Output is correct
3 Correct 725 ms 67992 KB Output is correct
4 Correct 339 ms 45992 KB Output is correct
5 Correct 720 ms 68056 KB Output is correct
6 Correct 722 ms 67916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23752 KB Output is correct
3 Correct 12 ms 23800 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23828 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 14 ms 23872 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 12 ms 23800 KB Output is correct
13 Correct 14 ms 23764 KB Output is correct
14 Correct 12 ms 23904 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23852 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 13 ms 23892 KB Output is correct
19 Correct 12 ms 23876 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23816 KB Output is correct
22 Correct 13 ms 23844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23752 KB Output is correct
3 Correct 12 ms 23800 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23828 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23764 KB Output is correct
8 Correct 12 ms 23832 KB Output is correct
9 Correct 14 ms 23872 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 269 ms 66816 KB Output is correct
12 Correct 240 ms 62552 KB Output is correct
13 Correct 725 ms 67992 KB Output is correct
14 Correct 339 ms 45992 KB Output is correct
15 Correct 720 ms 68056 KB Output is correct
16 Correct 722 ms 67916 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23800 KB Output is correct
19 Correct 14 ms 23764 KB Output is correct
20 Correct 12 ms 23904 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 12 ms 23852 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 13 ms 23892 KB Output is correct
25 Correct 12 ms 23876 KB Output is correct
26 Correct 13 ms 23764 KB Output is correct
27 Correct 13 ms 23816 KB Output is correct
28 Correct 13 ms 23844 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 301 ms 80132 KB Output is correct
31 Correct 273 ms 80204 KB Output is correct
32 Correct 400 ms 104012 KB Output is correct
33 Correct 304 ms 158568 KB Output is correct
34 Correct 732 ms 81212 KB Output is correct
35 Correct 742 ms 81224 KB Output is correct
36 Correct 764 ms 83676 KB Output is correct
37 Correct 765 ms 83496 KB Output is correct
38 Correct 529 ms 86332 KB Output is correct
39 Correct 565 ms 86536 KB Output is correct
40 Correct 558 ms 86356 KB Output is correct