Submission #127099

# Submission time Handle Problem Language Result Execution time Memory
127099 2019-07-08T22:03:25 Z Lawliet Mousetrap (CEOI17_mousetrap) C++14
45 / 100
1313 ms 70276 KB
#include <bits/stdc++.h>
 
#define MAX 1000010
 
using namespace std;
typedef long long int lli;
 
int n;
int mouse, trap;
int n1, n2;
 
int pai[MAX];
 
lli dp[MAX];
 
vector<int> grafo[MAX];
 
void DFSInit(int i, int p)
{
	for(int g = 0 ; g < grafo[i].size() ; g++)
	{
		int prox = grafo[i][g];
 
		if(prox == p) continue;
 
		pai[prox] = i;
 
		DFSInit(prox , i);
	}
}
 
void DFS(int i)
{
	if(grafo[i].size() == 1 && i != trap)
	{
		dp[i] = 0;
 
		return;
	}
 
	vector<lli> aux;
 
	aux.push_back( 0 );
 
	for(int g = 0 ; g < grafo[i].size() ; g++)
	{
		int prox = grafo[i][g];
 
		if(prox == pai[i]) continue;
 
		//printf("i = %d   chamei %d\n",i,prox);
 
		//printf("i = %d  prox = %d\n",i,prox);
 
		DFS(prox);
 
		aux.push_back( dp[prox] );
	}
 
	sort(aux.begin() , aux.end());
 
	dp[i] = grafo[i].size() - 2 + aux[aux.size() - 2] + 1;
 
	//printf("dp(%d) = %lld\n",i,dp[i]);
}
 
int main()
{
	scanf("%d %d %d",&n,&trap,&mouse);
 
	for(int g = 0 ; g < n - 1 ; g++)
	{
		scanf("%d %d",&n1,&n2);
 
		grafo[n1].push_back(n2);
		grafo[n2].push_back(n1);
	}
 
	if(trap == mouse)
	{
		printf("0\n");
		return 0;
	}

	DFSInit(trap , trap);
 
	lli ans = -1;
	int cur = mouse;
	int cnt = 0;
 
	while(cur != trap)
	{
		//printf("\n\n\n\n");
		DFS(trap);
 
		//printf("cur = %d\n",cur);
 
		int aux = pai[cur];
		int sum = 0;
 
		while(aux != trap)
		{
			sum += grafo[aux].size() - 2;
			//printf("aux = %d  sum = %d\n",aux,sum);
			aux = pai[aux];
		}
 
		//printf("dp = %d  sum = %d\n",dp[cur],sum);
 
		ans = max(ans , dp[cur] + max(0 , sum - cnt));
 
		for(int g = 0 ; g < grafo[pai[cur]].size() ; g++)
		{
			int prox = grafo[pai[cur]][g];
 
			if(prox == cur)
			{
				grafo[pai[cur]].erase(grafo[pai[cur]].begin() + g);
				break;
			}
		}
 
		cur = pai[cur];
		cnt++;
	}
 
	printf("%lld\n",ans);
}

Compilation message

mousetrap.cpp: In function 'void DFSInit(int, int)':
mousetrap.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int g = 0 ; g < grafo[i].size() ; g++)
                  ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void DFS(int)':
mousetrap.cpp:45:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int g = 0 ; g < grafo[i].size() ; g++)
                  ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:112:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int g = 0 ; g < grafo[pai[cur]].size() ; g++)
                   ~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&n,&trap,&mouse);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n1,&n2);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23860 KB Output is correct
4 Correct 23 ms 23788 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 24 ms 23908 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23848 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 69148 KB Output is correct
2 Correct 495 ms 64744 KB Output is correct
3 Correct 1253 ms 70188 KB Output is correct
4 Correct 670 ms 48192 KB Output is correct
5 Correct 1285 ms 70200 KB Output is correct
6 Correct 1313 ms 70276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23860 KB Output is correct
4 Correct 23 ms 23788 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 24 ms 23908 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23848 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Incorrect 27 ms 23800 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23860 KB Output is correct
4 Correct 23 ms 23788 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 24 ms 23908 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23848 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 560 ms 69148 KB Output is correct
12 Correct 495 ms 64744 KB Output is correct
13 Correct 1253 ms 70188 KB Output is correct
14 Correct 670 ms 48192 KB Output is correct
15 Correct 1285 ms 70200 KB Output is correct
16 Correct 1313 ms 70276 KB Output is correct
17 Incorrect 27 ms 23800 KB Output isn't correct
18 Halted 0 ms 0 KB -