답안 #303968

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303968 2020-09-20T20:49:05 Z peuch Mousetrap (CEOI17_mousetrap) C++17
25 / 100
1031 ms 72056 KB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1000010;

int n, t, m;
int pai[MAXN];
long long dp[MAXN];
int marc[MAXN];
long long ans;

vector<int> ar[MAXN];

void dfs(int cur);
void getDP(int cur);

int main(){
	scanf("%d %d %d", &n, &t, &m);
	for(int i = 1; i < n; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		ar[a].push_back(b);
		ar[b].push_back(a);
	}
	dfs(t);
	while(m != t){
		getDP(m);
		ans += dp[m];
		m = pai[m];
	}
	printf("%lld\n", ans);
}

void dfs(int cur){
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(viz == pai[cur]) continue;
		pai[viz] = cur;
		dfs(viz);
	}
}

void getDP(int cur){
	marc[cur] = 1;
	pair<long long, long long> aux = make_pair(0, 0);
	int tam = 0;
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(viz == pai[cur]) continue;
		if(marc[viz]) continue;
		getDP(viz);
		if(dp[viz] > aux.first) aux.second = aux.first, aux.first = dp[viz];
		else aux.second = max(aux.second, dp[viz]);
		tam++;
	}
	dp[cur] = aux.second + 1 + tam - 1;
	if(tam == 0) dp[cur] = 0;
}

Compilation message

mousetrap.cpp: In function 'void dfs(int)':
mousetrap.cpp:35:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void getDP(int)':
mousetrap.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |  scanf("%d %d %d", &n, &t, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Incorrect 15 ms 23808 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 420 ms 70912 KB Output is correct
2 Correct 381 ms 66168 KB Output is correct
3 Correct 1029 ms 72056 KB Output is correct
4 Correct 500 ms 47992 KB Output is correct
5 Correct 1020 ms 71928 KB Output is correct
6 Correct 1031 ms 71928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Incorrect 15 ms 23808 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 16 ms 23808 KB Output is correct
5 Incorrect 15 ms 23808 KB Output isn't correct
6 Halted 0 ms 0 KB -