#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;
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 |
6 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
26196 KB |
Output is correct |
2 |
Correct |
77 ms |
27112 KB |
Output is correct |
3 |
Correct |
73 ms |
27596 KB |
Output is correct |
4 |
Correct |
101 ms |
28096 KB |
Output is correct |
5 |
Correct |
82 ms |
25908 KB |
Output is correct |
6 |
Correct |
71 ms |
25820 KB |
Output is correct |
7 |
Correct |
68 ms |
27592 KB |
Output is correct |