Submission #103972

# Submission time Handle Problem Language Result Execution time Memory
103972 2019-04-03T13:19:25 Z luciocf Mousetrap (CEOI17_mousetrap) C++14
0 / 100
8 ms 1408 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 11;

int n, t, m;

int pai[maxn], nivel[maxn];

int dp[1<<maxn][maxn][2];

vector<pair<int, int>> grafo[maxn];

void dfs(int u, int p)
{
	for (auto pp: grafo[u])
	{
		int v = pp.first;
		if (v == p) continue;

		pai[v] = u, nivel[v] = nivel[u]+1;
		dfs(v, u);
	}
}

bool stuck(int mask, int u)
{
	for (auto v: grafo[u])
		if (v.first != pai[u] && !(mask&(1<<v.second))) return false;
	return true;
}

int lca(int u, int v)
{
	while (u != v)
	{
		if (nivel[u] > nivel[v]) u = pai[u];
		else v = pai[v];
	}

	return u;
}

int solve(int mask, int u, bool q)
{
	if (u == t) return 0;
	if (dp[mask][u][q] != -1) return dp[mask][u][q];

	if (stuck(mask, u))
	{
		int ans = 0, low = lca(u, t);

		int ant, w = u;

		while (w != low)
		{
			for (auto v: grafo[w])
			{
				if (v.first == pai[w] || v.first == ant) continue;
				
				if (!(mask&(1<<v.second)))
					ans++;
			}
				
			ant = w;
			w = pai[w], ans++;
		}

		if (t == low) return dp[mask][u][q] = ans;

		ant = 0, w = t;

		while (w != low)
		{
			for (auto v: grafo[w])
			{
				if (v.first == pai[w] || v.first == ant) continue;
				
				if (!(mask&(1<<v.second)))
					ans++;
			}
				
			ant = w;
			w = pai[w], ans++;
		}

		return dp[mask][u][q] = ans;
	}

	int ans = 0;
	
	if (q)
	{
		ans = solve(mask, u, 0);

		for (int i = 0; i < n-1; i++)
			if (!(mask&(1<<i))) 
				ans = min(ans, 1+solve(mask|(1<<i), u, 0));

		return dp[mask][u][q] = ans;
	}

	for (auto v: grafo[u])
	{
		if (v.first == pai[u]) continue;
		if ((mask&(1<<v.second))) continue;

		ans = max(ans, solve(mask, v.first, 1));
	}

	return dp[mask][u][q] = ans;
}

int main(void)
{
	scanf("%d %d %d", &n, &t, &m);

	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back({v, i});
		grafo[v].push_back({u, i});
	}

	dfs(m, 0);

	memset(dp, -1, sizeof dp);
	printf("%d\n", solve(0, m, 1));
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:117: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:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int solve(int, int, bool)':
mousetrap.cpp:60:27: warning: 'ant' may be used uninitialized in this function [-Wmaybe-uninitialized]
     if (v.first == pai[w] || v.first == ant) continue;
         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Incorrect 3 ms 512 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 1408 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Incorrect 3 ms 512 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Incorrect 3 ms 512 KB Output isn't correct
9 Halted 0 ms 0 KB -