This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef string str;
const ll Mod = 1e9 + 7;
const int Maxn = 2e5 + 100;
vector<ll> G[Maxn];
ll dp[Maxn];
ll t, m;
bool DFS(ll u, ll p){
bool res = (u == m);
ll cnt = 0;
ll mx = 0;
ll mx2 = 0;
for(auto adj : G[u]){
if(adj == p) continue;
res |= DFS(adj, u);
cnt ++;
if(dp[adj] > mx){
mx2 = mx;
mx = dp[adj];
} else mx2 = max(mx2, dp[adj]);
}
dp[u] = cnt + mx2;
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;
cin >> n >> t >> m;
ll u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
DFS(t, -1);
cout << dp[m] << '\n';
return 0;
}
/*
5 1 2
1 2
2 3
2 4
3 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |