Submission #584872

# Submission time Handle Problem Language Result Execution time Memory
584872 2022-06-28T05:57:25 Z amunduzbaev Mousetrap (CEOI17_mousetrap) C++17
0 / 100
332 ms 76268 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];
		if(!is[x]) t.push_back(x);
	}
	
	sort(t.begin(), t.end(), [&](int& i, int& j){
		return dp[i] > dp[j];
	});
	if(is[u] && u != ::t) res += (int)t.size() - 1;
	
	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);
	cout<<res + dp[m]<<"\n";
}

# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 332 ms 76268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23740 KB Output isn't correct
2 Halted 0 ms 0 KB -