Submission #96368

# Submission time Handle Problem Language Result Execution time Memory
96368 2019-02-08T18:33:00 Z Bruteforceman Mousetrap (CEOI17_mousetrap) C++11
25 / 100
1206 ms 72192 KB
#include "bits/stdc++.h"
using namespace std;
vector <int> g[1000010];
int dp[1000010], cn[1000010];
int dep[1000010];
int m, t;

void dfs(int x, int par) {
	if(x == t) {
		cn[x] = 1;
		dp[x] = 0;
		dep[x] = 0;
		return ;
	}
	int deg = 0;
	int sib = 0;
	for(auto i : g[x]) {
		if(i - par) {
			dfs(i, x);
			cn[x] |= cn[i];
			++deg;
			if(cn[i]) {
				dep[x] = dep[i];
				sib = i;
			}
		}
	}
	dep[x] += deg - 1;
	vector <int> can;
	for(auto i : g[x]) {
		if(i - par) {
			if(cn[i]) {
				can.emplace_back(dp[i]);
			} else {
				if(cn[x]) {
					can.emplace_back(dp[i] + dep[x]);
				} else {
					can.emplace_back(dp[i] + deg);
				}
			}
		}
	}
	sort(can.begin(), can.end());
	reverse(can.begin(), can.end());
	if(cn[x]) {
		dp[x] = dp[sib];
	} else {
		dp[x] = 0;
	}
	if(can.size() > 1) {
		dp[x] = max(dp[x], can[1]);
	} else if (!cn[x] && can.size() == 1) {
		dp[x] = 1;		
	}
}

bool good(int x) {
	int cur = m;
	int prv = -1;
	int flux = 0;
	while(cur != t) {
		++flux;
		for(auto i : g[cur]) {
			if(!cn[i]) {
				if(dep[cur] + dp[i] >= x) {
					--flux;
				}
			}
		}
		if(flux < 0) return true;
		for(auto i : g[cur]) {
			if(cn[i] && i != prv) {
				prv = cur;
				cur = i;
				break;
			}
		}
	}
	return false;
}

int search(int b, int e) {
	if(b == e) {
		return b;
	}
	int m = (b + e + 1) >> 1;
	if(good(m)) {
		return search(m, e);
	} else {
		return search(b, m-1);
	}
}

int main(int argc, char const *argv[])
{
	int n;
	scanf("%d %d %d", &n, &t, &m);
	for(int i = 1; i < n; i++) {
		int p, q;
		scanf("%d %d", &p, &q);
		g[p].emplace_back(q);
		g[q].emplace_back(p);
	}
	dfs(m, 0);
	printf("%d\n", search(0, n));
	return 0;
}

Compilation message

mousetrap.cpp: In function 'int main(int, const char**)':
mousetrap.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &t, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 481 ms 69040 KB Output is correct
2 Correct 411 ms 65144 KB Output is correct
3 Correct 1031 ms 72124 KB Output is correct
4 Correct 463 ms 52344 KB Output is correct
5 Correct 1079 ms 72192 KB Output is correct
6 Correct 1206 ms 72176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -