답안 #303060

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303060 2020-09-19T20:25:22 Z Kenzo_1114 Mousetrap (CEOI17_mousetrap) C++17
45 / 100
911 ms 64148 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;

int n, t, m, dp[MAXN], par[MAXN];
vector<int> graph[MAXN];

void dfs(int v, int p)
{	
	int mx[2] = {-1, -1};
	par[v] = p;

	for(int i = 0; i < graph[v].size(); i++)
	{
		int u = graph[v][i];
		if(u == p)	continue;

		dfs(u, v);
		if(dp[u] >= mx[0])	mx[1] = mx[0], mx[0] = dp[u];
		else if(dp[u] > mx[1])	mx[1] = dp[u];
	}

	dp[v] = ((int) graph[v].size() == 2) ? 1 : mx[1] + graph[v].size() - 1;
	if((int) graph[v].size() == 1)	dp[v] = 0;
}

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

	for(int i = 0, u, v; i < n - 1; i++)
	{
		scanf("%d %d", &u, &v);
		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	if(t == m || (int) graph[m].size() == 0)
	{	printf("0\n"); return 0;	}

	dfs(t, t);

	int cur = m, ans = dp[m];
	while(true) 
	{
		cur = par[cur];
		if(cur == t)	break;
		ans += (int) graph[cur].size() - 2;
	}

//	for(int i = 1; i <= n; i++)	printf("dp[%d] = %d\n", i, dp[i]);

	printf("%d\n", ans);
}

/*

10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10

*/

Compilation message

mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:13:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |  for(int i = 0; i < graph[v].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |  scanf("%d %d %d", &n, &t, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 412 ms 63096 KB Output is correct
2 Correct 382 ms 59012 KB Output is correct
3 Correct 887 ms 63992 KB Output is correct
4 Correct 415 ms 43896 KB Output is correct
5 Correct 894 ms 64148 KB Output is correct
6 Correct 911 ms 63992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23768 KB Output is correct
11 Incorrect 16 ms 23808 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 16 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23768 KB Output is correct
11 Correct 412 ms 63096 KB Output is correct
12 Correct 382 ms 59012 KB Output is correct
13 Correct 887 ms 63992 KB Output is correct
14 Correct 415 ms 43896 KB Output is correct
15 Correct 894 ms 64148 KB Output is correct
16 Correct 911 ms 63992 KB Output is correct
17 Incorrect 16 ms 23808 KB Output isn't correct
18 Halted 0 ms 0 KB -