답안 #308781

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
308781 2020-10-01T23:27:52 Z wmrmr Mousetrap (CEOI17_mousetrap) C++17
25 / 100
5000 ms 77372 KB
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+10;
int dp[MAX], pai[MAX];
int n,t,m;
vector<int> g[MAX];

void DFS1(int v, int p)
{
	pai[v] = p;
	int grau = g[v].size();
	pair<int,int> mx; mx = {-1,-1};
	if(grau==1) return;
	if(grau==2){
		dp[v] = 1; return; }
	for(int i=0;i<grau;i++)
	{
		int prox = g[v][i];
		if(prox == p) continue;
		DFS1(prox,v);
		if(dp[prox] >= mx.first)
			mx.second = mx.first, mx.first = dp[prox];
		else if(dp[prox] > mx.second)
			mx.second = dp[prox];
	}
	dp[v] = grau + mx.second - 1;
}

void DFS2(int v, int f, int ans)
{
	if(v == t) {
		printf("%d",ans); return; }
	if(f == 0) {
		DFS2(pai[v],v,dp[v]); return; }
	int grau = g[v].size();
	if(grau == 2) {
		DFS2(pai[v],v,ans); return; }
	if(grau == 3) {
		DFS2(pai[v],v,ans+1); return; }
	pair<int,int> mx; mx = {-1,-1};
	for(int i=0;i<g[v].size();i++)
	{
		int prox = g[v][i];
		if(prox == f || prox == pai[v]) continue;
		if(dp[prox] >= mx.first)
			mx.second = mx.first, mx.first = dp[prox];
		else if(dp[prox] > mx.second)
			mx.second = dp[prox];
	}
	DFS2(pai[v],v,ans+grau+mx.second-2);
}

int main()
{
	scanf("%d %d %d",&n,&t,&m);
	for(int i=1;i<n;i++)
	{
		int a,b; scanf("%d %d",&a,&b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i=0;i<g[t].size();i++)
	{
		int prox = g[t][i];
		DFS1(prox,t);
	}
	DFS2(m,0,0);
}

Compilation message

mousetrap.cpp: In function 'void DFS2(int, int, int)':
mousetrap.cpp:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for(int i=0;i<g[v].size();i++)
      |              ~^~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for(int i=0;i<g[t].size();i++)
      |              ~^~~~~~~~~~~~
mousetrap.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   55 |  scanf("%d %d %d",&n,&t,&m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:58:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   58 |   int a,b; scanf("%d %d",&a,&b);
      |            ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Execution timed out 5066 ms 23808 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 61048 KB Output is correct
2 Correct 378 ms 57336 KB Output is correct
3 Correct 800 ms 64120 KB Output is correct
4 Correct 405 ms 50352 KB Output is correct
5 Correct 809 ms 77308 KB Output is correct
6 Correct 834 ms 77372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Execution timed out 5066 ms 23808 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Execution timed out 5066 ms 23808 KB Time limit exceeded
4 Halted 0 ms 0 KB -