#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]=mx[node][1]={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][1].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];
cout<<ans<<'\n';
//for (int i=2; i<=n; i++) debug2(i, val[i])
//for (int i=2; i<=n; i++) debug2(i, dp[i])
return 0;
}
/*
10 1 2
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
5 1 2
1 2
2 3
2 4
2 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
439 ms |
86416 KB |
Output is correct |
2 |
Correct |
417 ms |
92280 KB |
Output is correct |
3 |
Correct |
1432 ms |
100852 KB |
Output is correct |
4 |
Correct |
640 ms |
62200 KB |
Output is correct |
5 |
Correct |
1423 ms |
100852 KB |
Output is correct |
6 |
Correct |
1382 ms |
100876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |