#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dep[300005];
int chcnt[300005];
int col[300005];
int dp[300005];
int dfs(int u, int p = -1) {
vector<int> vec;
for (int v : g[u]) {
if (v == p)
continue;
dfs(v, u);
vec.push_back(dp[v]);
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
dp[u] = max(dp[u], vec[i] + i + 1);
}
return dp[u];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
if (a == b || n < 2)
while (true)
;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
memset(dep, 0x3f, sizeof(dep));
dep[a] = dep[b] = 0;
col[a] = 0;
col[b] = 1;
queue<int> q;
q.push(a);
q.push(b);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (dep[v] > dep[u] + 1) {
dep[v] = dep[u] + 1;
q.push(v);
col[v] = col[u];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j : g[i]) {
if (col[j] != col[i]) {
g[i].erase(find(g[i].begin(), g[i].end(), j));
break;
}
}
}
cout << max(dfs(a), dfs(b)) << endl;
return 0;
}
Compilation message
torrent.cpp: In function 'int dfs(int, int)':
torrent.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
11864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
26196 KB |
Output is correct |
2 |
Correct |
77 ms |
27096 KB |
Output is correct |
3 |
Correct |
85 ms |
27456 KB |
Output is correct |
4 |
Correct |
100 ms |
27740 KB |
Output is correct |
5 |
Correct |
88 ms |
25432 KB |
Output is correct |
6 |
Correct |
79 ms |
25604 KB |
Output is correct |
7 |
Correct |
73 ms |
27500 KB |
Output is correct |