# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1067895 |
2024-08-21T05:22:05 Z |
Plurm |
Torrent (COI16_torrent) |
C++11 |
|
137 ms |
24992 KB |
#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({1, 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++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
22096 KB |
Output is correct |
2 |
Correct |
81 ms |
23076 KB |
Output is correct |
3 |
Correct |
72 ms |
23332 KB |
Output is correct |
4 |
Correct |
77 ms |
24992 KB |
Output is correct |
5 |
Correct |
80 ms |
21588 KB |
Output is correct |
6 |
Correct |
71 ms |
21708 KB |
Output is correct |
7 |
Correct |
71 ms |
23504 KB |
Output is correct |