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 X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 2e5+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 20;
const int off = 1 << logo;
const int treesiz = off << 1;
int n, a, b;
vector< int > graph[maxn];
int dp[maxn];
vector< int > v;
bool dfs(int x, int parr) {
bool out = (x == b);
for (int i = 0; i < graph[x].size(); i++) {
int tren = graph[x][i];
if (tren == parr) continue;
out |= dfs(tren, x);
}
if (out) v.push_back(x);
return out;
}
int ax, ay;
bool check(int x, int y) {
if (x == ax && y == ay) return false;
if (x == ay && y == ax) return false;
return true;
}
int f(int x, int parr) {
int out = 0;
vector< int > v;
for (int i = 0; i < graph[x].size(); i++) {
int tren = graph[x][i];
if (tren == parr || !check(x, tren)) continue;
v.push_back(f(tren, x));
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
out = max(out, i + 1 + v[i]);
}
return out;
}
pair<int, int> solve(int x, int y) {
ax = x, ay = y;
int aa = f(a, -1);
int bb = f(b, -1);
return make_pair(aa, bb);
}
int main() {
scanf("%d%d%d", &n, &a, &b);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(a, -1);
reverse(v.begin(), v.end());
int lo = 0, hi = v.size() - 2;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
pair<int, int> tren = solve(v[mid], v[mid + 1]);
if (tren.X <= tren.Y) lo = mid;
else hi = mid - 1;
}
//for (int i = 0; i < v.size(); i++) printf("%d ", v[i]); printf("\n");
int sol = inf;
for (int i = -1; i < 2; i++) {
int tr = lo + i;
if (tr >= 0 && tr + 1 <= v.size() - 1) {
pair<int, int> tren = solve(v[tr], v[tr + 1]);
//printf("trying: %d %d\n", v[tr], v[tr + 1]);
//printf("debug: %d %d\n", tren.X, tren.Y);
sol = min(sol, max(tren.X, tren.Y));
}
}
printf("%d\n", sol);
return 0;
}
Compilation message (stderr)
torrent.cpp: In function 'bool dfs(int, int)':
torrent.cpp:23:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < graph[x].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
torrent.cpp: In function 'int f(int, int)':
torrent.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < graph[x].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
torrent.cpp:51:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:89:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (tr >= 0 && tr + 1 <= v.size() - 1) {
| ~~~~~~~^~~~~~~~~~~~~~~
torrent.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d%d%d", &n, &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |