/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 22;
int n, s, t, dp[N], pd[N];
ll ans = 0;
vector<int> g[N];
bool dfs(int v, int p){
if(v == t) return 1;
bool ok = 0;
dp[v] = 0;
pd[v] = 0;
vector<int> dps;
int child_t = -1;
for(int u: g[v]){
if(u != p){
bool b = dfs(u, v);
if(!b)
dps.pb(dp[u] + 1);
if(b)
child_t = u;
ok |= b;
}
}
sort(all(dps), greater<int>());
if(ok){
pd[v] = pd[child_t] + int(dps.size());
if(dps.size() == 0)
dp[v] = dp[child_t];
else if(dps.size() == 1)
dp[v] = 1 + dp[child_t];
else
dp[v] = max(dps[1] + pd[v] - 1, dp[child_t] + 1);
}else{
dp[v] = dps.size() <= 1 ? int(dps.size()) : dps[1] + (dps.size() - 1);
}
// cout << dp[v] << ' ' << v << ' ' << pd[v] << '\n';
return ok;
}
void solve(){
cin >> n >> t >> s;
for(int i = 0; i < n - 1; ++i){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dp[t] = pd[t] = 0;
dfs(s, 0);
cout << dp[s];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:63:15: warning: unused variable 'aa' [-Wunused-variable]
63 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27268 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
63052 KB |
Output is correct |
2 |
Correct |
227 ms |
59364 KB |
Output is correct |
3 |
Correct |
601 ms |
64200 KB |
Output is correct |
4 |
Correct |
245 ms |
47444 KB |
Output is correct |
5 |
Correct |
618 ms |
64172 KB |
Output is correct |
6 |
Correct |
593 ms |
64160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27268 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27268 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |