This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: 5ak0
PROG:
LANG: C++11
*/
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define mpr make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 7, MAXN = 3e5 + 10;
int a, b;
int n;
int x, y, res;
int d[MAXN], cnt[MAXN];
bool u[MAXN];
vector <int> g[MAXN];
int main(){
cin >> n >> a >> b;
for (int i = 1; i < n; ++i){
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
queue <int> q;
q.push(a);
q.push(b);
u[a] = 1;
u[b] = 1;
while (!q.empty()){
int v = q.front();
q.pop();
++cnt[v];
int mx = -1, mxi = 0;
for (auto to : g[v]){
if (u[to] == 0){
int cnt = 0;
for (auto too : g[to])
cnt += (u[too] == 0);
if (cnt > mx)
mx = cnt, mxi = to;
}
}
if (mx != -1){
q.push(v);
q.push(mxi);
u[mxi] = 1;
d[mxi] = d[v] + cnt[v];
}
}
for (int i = 1; i <= n; ++i)
// cerr << d[i] << " ";
res = max(res, d[i]);
cout << res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |