답안 #200245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
200245 2020-02-05T18:22:47 Z alishahali1382 Mousetrap (CEOI17_mousetrap) C++14
0 / 100
440 ms 99836 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans, root, mouse;
int par[MAXN], h[MAXN];
int val[MAXN], dp[MAXN];
pii mx[MAXN][2];
vector<int> G[MAXN];

void dfs1(int node){
	for (int v:G[node]) if (v!=par[node]){
		par[v]=node;
		h[v]=h[node]+1;
		dfs1(v);
	}
}

void dfs2(int node){
	val[node]=val[par[node]];
	for (int v:G[node]) if (v!=par[node]) val[node]++;
	for (int v:G[node]) if (v!=par[node]) dfs2(v);
}

void dfs3(int node){
	mx[node][0]={val[node], node};
	for (int v:G[node]) if (v!=par[node]){
		dfs3(v);
		pii p={dp[v], v};
		if (mx[node][0]<p) swap(mx[node][0], p);
		if (mx[node][1]<p) swap(mx[node][1], p);
		//if (mx[node][2]<p) swap(mx[node][2], p);
	}
	dp[node]=mx[node][0].first;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>root>>mouse;
	for (int i=1; i<n; i++){
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs1(root);
	for (root=mouse; par[par[root]]; root=par[root]);
	dfs2(root);
	dfs3(root);
	
	ans=dp[mouse]-1;
	cout<<ans<<'\n';
	
	//for (int i=2; i<=n; i++) debug2(i, val[i])
	
	return 0;
}
/*
10 1 2
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 440 ms 99836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 23800 KB Output isn't correct
2 Halted 0 ms 0 KB -