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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <int> neigh[300000];
int lg = 19;
pair <int, int> block;
int time(int p, int c) {
int max = 0;
int x = 1;
vector <int> tms;
for(int i = 0; i < neigh[c].size(); i++) {
pair <int, int> pa1 = {.first = neigh[c][i], .second = c};
pair <int, int> pa2 = {.first = c, .second = neigh[c][i]};
//cout << "blocked: " << block.first << "," << block.second << "\n";
if(neigh[c][i] != p && block != pa1 && block != pa2) {
//cout << "iz " << c << " idemo na " << pa1.first << "," << pa1.second << "\n";
tms.push_back(time(c, neigh[c][i]));
}
}
//cout << "Cvor " << c + 1 << ": ";
sort(tms.begin(), tms.end());
reverse(tms.begin(), tms.end());
for(int i = 0; i < tms.size(); i++) {
if(max < tms[i] + x) max = tms[i] + x;
// cout << tms[i] + x << ' ';
x++;
}
//cout << '\n';
return max;
}
int plen;
bool dfsdone = false;
void dfs(int p, int c, int r, int path[], int len) {
path[len] = c;
if(c == r) {
dfsdone = true;
plen = len + 1;
return;
}
for(int i = 0; i < neigh[c].size(); i++) {
if(neigh[c][i] != p) {
dfs(c, neigh[c][i], r, path, len + 1);
if(dfsdone) return;
}
}
}
void blockln(int a, int b) {
block.first = a;
block.second = b;
}
int main() {
int n, a, b, path[100000];
cin >> n >> a >> b;
a--;
b--;
for(int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
neigh[x - 1].push_back(y - 1);
neigh[y - 1].push_back(x - 1);
}
/*cout << time(a, a);
return 0;*/
dfs(a, a, b, path, 0);
// cout << "Path: ";
//for(int i = 0; i < plen; i++) {
// cout << path[i] << " ";
// }
// cout << "\n";
int l = 0;
int r = plen - 2;
int sa, sb, mi;
while(l < r) {
mi = (l + r) / 2;
blockln(path[mi], path[mi + 1]);
// cout << "Racunamo A\n";
sa = time(a, a);
// cout << "Racunamo B\n";
sb = time(b, b);
if(sa > sb) r = mi;
else l = mi + 1;
}
blockln(path[r], path[r + 1]);
sa = time(a, a);
int max1 = sa;
if(r == 0) {
// cout << "jeeeej\n";
cout << max1;
return 0;
}
blockln(path[r], path[r - 1]);
sb = time(b, b);
int max2 = sb;
if(max1 < max2) cout << max1;
else cout << max2;
return 0;
}
Compilation message (stderr)
torrent.cpp: In function 'int time(int, int)':
torrent.cpp:15:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < neigh[c].size(); i++) {
~~^~~~~~~~~~~~~~~~~
torrent.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < tms.size(); i++) {
~~^~~~~~~~~~~~
torrent.cpp: In function 'void dfs(int, int, int, int*, int)':
torrent.cpp:48:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < neigh[c].size(); i++) {
~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |