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>
using namespace std;
#define ll long long
vector<vector<int>> graph;
vector<ll> opt;
vector<int> path;
int f, s;
bool pos = 1;
int border = -1;
void dfs(int i, int p, ll limit)
{
if (i==s) return;
vector<ll> subs;
int alt = -1;
for (int ele:graph[i])
{
if (ele==p) continue;
if (!path[ele])
{
dfs(ele, i, limit);
subs.push_back(opt[ele]);
}
else alt = ele;
}
sort(subs.begin(), subs.end(), greater<ll>());
int c = 0;
for (int j = 0; j < subs.size(); j++)
{
if (subs[j]+j+1==limit) c++;
opt[i] = max(opt[i], subs[j]+j+1);
}
if (opt[i]>limit)
{
if (path[i]) border = p;
return;
}
if (alt != -1) dfs(alt, i, limit-c-1);
}
void check(int i, int p)
{
vector<ll> subs;
for (int ele:graph[i])
{
if (ele == p) continue;
if (ele == border) continue;
check(ele, i);
subs.push_back(opt[ele]);
}
sort(subs.begin(), subs.end(), greater<ll>());
for (int j = 0; j < subs.size(); j++)
{
opt[i] = max(opt[i], subs[j]+j+1);
}
}
int findpath(int i, int p)
{
if (i==f) return 1;
for (int ele:graph[i])
{
if (ele==p) continue;
if (findpath(ele, i)) path[i] = 1;
}
return path[i];
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
cin >> n >> f >> s;
f--; s--;
graph.assign(n, vector<int>());
path.assign(n, 0);
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
findpath(s, -1);
ll sol = 0;
ll one = 1;
for (int i = 40; i >= 0; i--)
{
opt.assign(n, 0);
pos = 1;
border = f;
sol += one<<i;
dfs(f, -1, sol);
if (opt[f]>sol) pos = 0;
opt.assign(n, 0);
check(s, -1);
if (opt[s]>sol) pos = 0;
if (pos) sol -= one<<i;
}
cout << sol+1;
return 0;
}
Compilation message (stderr)
torrent.cpp: In function 'void dfs(int, int, long long int)':
torrent.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int j = 0; j < subs.size(); j++)
| ~~^~~~~~~~~~~~~
torrent.cpp: In function 'void check(int, int)':
torrent.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int j = 0; j < subs.size(); j++)
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |