답안 #563755

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563755 2022-05-18T05:13:16 Z ngpin04 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
815 ms 145056 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;
          	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 12 ms 23732 KB Output is correct
3 Correct 13 ms 23744 KB Output is correct
4 Correct 13 ms 23832 KB Output is correct
5 Correct 13 ms 23784 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 13 ms 23752 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 13 ms 23720 KB Output is correct
10 Correct 15 ms 23828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 289 ms 66836 KB Output is correct
2 Correct 253 ms 62488 KB Output is correct
3 Correct 781 ms 68040 KB Output is correct
4 Correct 355 ms 45940 KB Output is correct
5 Correct 778 ms 67972 KB Output is correct
6 Correct 788 ms 68064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23732 KB Output is correct
3 Correct 13 ms 23744 KB Output is correct
4 Correct 13 ms 23832 KB Output is correct
5 Correct 13 ms 23784 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 13 ms 23752 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 13 ms 23720 KB Output is correct
10 Correct 15 ms 23828 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 16 ms 23844 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23892 KB Output is correct
16 Correct 14 ms 23840 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 14 ms 23792 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23800 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23732 KB Output is correct
3 Correct 13 ms 23744 KB Output is correct
4 Correct 13 ms 23832 KB Output is correct
5 Correct 13 ms 23784 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 13 ms 23752 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 13 ms 23720 KB Output is correct
10 Correct 15 ms 23828 KB Output is correct
11 Correct 289 ms 66836 KB Output is correct
12 Correct 253 ms 62488 KB Output is correct
13 Correct 781 ms 68040 KB Output is correct
14 Correct 355 ms 45940 KB Output is correct
15 Correct 778 ms 67972 KB Output is correct
16 Correct 788 ms 68064 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 16 ms 23844 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 14 ms 23840 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 14 ms 23792 KB Output is correct
25 Correct 13 ms 23764 KB Output is correct
26 Correct 13 ms 23764 KB Output is correct
27 Correct 13 ms 23800 KB Output is correct
28 Correct 13 ms 23764 KB Output is correct
29 Correct 12 ms 23764 KB Output is correct
30 Correct 292 ms 66732 KB Output is correct
31 Correct 280 ms 66772 KB Output is correct
32 Correct 367 ms 90692 KB Output is correct
33 Correct 323 ms 145056 KB Output is correct
34 Correct 787 ms 67976 KB Output is correct
35 Correct 812 ms 67992 KB Output is correct
36 Correct 810 ms 70284 KB Output is correct
37 Correct 815 ms 70340 KB Output is correct
38 Correct 556 ms 72960 KB Output is correct
39 Correct 563 ms 72984 KB Output is correct
40 Correct 582 ms 72920 KB Output is correct