답안 #584897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
584897 2022-06-28T06:28:11 Z amunduzbaev Mousetrap (CEOI17_mousetrap) C++17
45 / 100
823 ms 77260 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;

const int N = 1e6 + 5;
vector<int> edges[N];
int n, t, m, dp[N], is[N];
int res = 0;

void dfs(int u, int p = -1){
	is[u] = (u == t);
	vector<int> t;
	for(auto x : edges[u]){
		if(x == p) continue;
		dfs(x, u);
		is[u] |= is[x];
		t.push_back(x);
	}
	edges[u] = t;
	sort(t.begin(), t.end(), [&](int& i, int& j){
		return dp[i] > dp[j];
	});
	
	if(t.empty()) dp[u] = 0;
	else {
		dp[u] = 1 + ((int)t.size() > 1 ? dp[t[1]] + 1 : 0) + max(0, (int)t.size() - 2);
	}
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	cin>>n>>t>>m;
	if(t == m){
		cout<<0<<"\n";
		return 0;
	}
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	dfs(m);
	int pos = 0, res = 0;
	while(m != t){
		//~ cout<<m<<" "<<t<<endl;
		pos++;
		vector<int> t;
		int c = -1;
		for(auto x : edges[m]){
			if(is[x]) { c = x; continue; }
			t.push_back(x);
		}
		
		sort(t.begin(), t.end(), [&](int& i, int& j){
			return dp[i] > dp[j];
		});
		if(pos >= (int)t.size()){
			pos -= (int)t.size();
			res += (int)t.size();
		} else {
			res += dp[t[pos]] + (int)t.size();
			pos = 1e9;
		}
		m = c;
	}
	
	cout<<res<<"\n";
}

/*

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

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 14 ms 23816 KB Output is correct
3 Correct 13 ms 23816 KB Output is correct
4 Correct 13 ms 23708 KB Output is correct
5 Correct 13 ms 23812 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 14 ms 23812 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23816 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 302 ms 62824 KB Output is correct
2 Correct 283 ms 70900 KB Output is correct
3 Correct 750 ms 77208 KB Output is correct
4 Correct 375 ms 50344 KB Output is correct
5 Correct 823 ms 77260 KB Output is correct
6 Correct 781 ms 77204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 14 ms 23816 KB Output is correct
3 Correct 13 ms 23816 KB Output is correct
4 Correct 13 ms 23708 KB Output is correct
5 Correct 13 ms 23812 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 14 ms 23812 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23816 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Incorrect 12 ms 23764 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23764 KB Output is correct
2 Correct 14 ms 23816 KB Output is correct
3 Correct 13 ms 23816 KB Output is correct
4 Correct 13 ms 23708 KB Output is correct
5 Correct 13 ms 23812 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 14 ms 23812 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23816 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 302 ms 62824 KB Output is correct
12 Correct 283 ms 70900 KB Output is correct
13 Correct 750 ms 77208 KB Output is correct
14 Correct 375 ms 50344 KB Output is correct
15 Correct 823 ms 77260 KB Output is correct
16 Correct 781 ms 77204 KB Output is correct
17 Incorrect 12 ms 23764 KB Output isn't correct
18 Halted 0 ms 0 KB -