# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
846682 |
2023-09-08T08:58:39 Z |
vjudge1 |
Torrent (COI16_torrent) |
C++17 |
|
329 ms |
27064 KB |
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define task "Torrent"
using namespace std;
const int N = 3e5 + 2;
int n, a, b, x, y, dp1[N], dp2[N];
vector<int> vt[N], trace;
void find(int u, int v)
{
for (auto i : vt[u])
{
if (i == v)
continue;
if (i == b)
{
trace.push_back(i);
trace.push_back(u);
return;
}
find(i, u);
if (trace.size() != 0 && trace.back() == i)
{
trace.push_back(u);
return;
}
}
}
void dfs1(int u, int v)
{
if ((u == x && v == y) || (v == x && u == y))
return;
vector<int> tmp;
for (auto i : vt[u])
{
if ((u == x && i == y) || (i == x && u == y))
continue;
if (i == v)
continue;
dfs1(i, u);
tmp.push_back(dp1[i]);
}
sort(tmp.begin(), tmp.end(), greater<int>());
dp1[u] = 0;
for (int k = 0; k < tmp.size(); k++)
dp1[u] = max(dp1[u], k + 1 + tmp[k]);
// cout << u << " " << dp1[u] << '\n';
return;
}
void dfs2(int u, int v)
{
if ((u == x && v == y) || (v == x && u == y))
return;
vector<int> tmp;
for (auto i : vt[u])
{
if ((u == x && i == y) || (i == x && u == y))
continue;
if (i == v)
continue;
dfs2(i, u);
tmp.push_back(dp2[i]);
}
sort(tmp.begin(), tmp.end(), greater<int>());
dp2[u] = 0;
for (int k = 0; k < tmp.size(); k++)
dp2[u] = max(dp2[u], k + 1 + tmp[k]);
// cout << u << " " << dp2[u] << '\n';
return;
}
void check(int tmp1, int tmp2)
{
x = tmp1;
y = tmp2;
dfs1(a, 0);
// cout << '\n';
dfs2(b, 0);
}
int main()
{
// freopen(task ".inp", "r", stdin);
// freopen(task ".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> a >> b;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
vt[u].push_back(v);
vt[v].push_back(u);
}
find(a, 0);
// trace.pop_back();
reverse(trace.begin(), trace.end());
if (n <= 1000)
{
int ans = 1e9;
for (int mid = 1; mid < trace.size(); mid++)
{
check(trace[mid - 1], trace[mid]);
ans = min(ans, max(dp1[a], dp2[b]));
}
cout << ans;
return 0;
}
int low = 1, high = trace.size() - 1, mid, ans = 1e9;
while (low <= high)
{
mid = (low + high) / 2;
check(trace[mid - 1], trace[mid]);
if (dp1[a] > dp2[b])
{
ans = min(ans, max(dp1[a], dp2[b]));
low = mid + 1;
}
else
{
ans = min(ans, max(dp1[a], dp2[b]));
high = mid - 1;
}
}
cout << ans;
return 0;
}
Compilation message
torrent.cpp: In function 'void dfs1(int, int)':
torrent.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int k = 0; k < tmp.size(); k++)
| ~~^~~~~~~~~~~~
torrent.cpp: In function 'void dfs2(int, int)':
torrent.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int k = 0; k < tmp.size(); k++)
| ~~^~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:110:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int mid = 1; mid < trace.size(); mid++)
| ~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8796 KB |
Output is correct |
2 |
Correct |
5 ms |
8796 KB |
Output is correct |
3 |
Correct |
7 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
329 ms |
21840 KB |
Output is correct |
2 |
Correct |
283 ms |
23592 KB |
Output is correct |
3 |
Correct |
297 ms |
26276 KB |
Output is correct |
4 |
Correct |
268 ms |
24268 KB |
Output is correct |
5 |
Correct |
308 ms |
21476 KB |
Output is correct |
6 |
Correct |
285 ms |
22108 KB |
Output is correct |
7 |
Correct |
236 ms |
27064 KB |
Output is correct |