# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
846671 |
2023-09-08T08:48:06 Z |
vjudge1 |
Torrent (COI16_torrent) |
C++17 |
|
289 ms |
20992 KB |
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
vector<pair<int, int>> path;
int A, B, trace[N];
void dfs(int u, int p)
{
for(auto v: adj[u])
{
if(v == p) continue;
trace[v] = u;
dfs(v, u);
}
}
int DFS(int u, int p, pair<int, int> block) {
int sum = 0;
vector<int> res;
for(int v : adj[u])
{
if(v == p)
continue;
if(make_pair(u, v) == block || make_pair(v, u) == block)
continue;
res.push_back(DFS(v, u, block));
}
sort(res.begin(), res.end());
reverse(res.begin(), res.end());
for(int i = 0; i < res.size(); i++)
sum = max(sum, res[i] + (i + 1));
return sum;
}
bool Check(int mid)
{
//check coi max cay a > max cay b ko??
pair<int, int> block = path[mid];
return DFS(A, 0, block) > DFS(B, 0, block);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> A >> B;
for(int i = 1; i < n; i ++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(A, 0);
int x = B;
while(x != A)
{
path.push_back({trace[x], x});
x = trace[x];
}
// for(auto tmp: path)
// {
// cout << tmp.first << ' ' << tmp.second << endl;
// }
int l = 0, r = path.size() - 1, ans = 0;
while(l <= r)
{
int mid = (l + r) / 2;
if(Check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
int res = max(DFS(A, 0, path[ans]), DFS(B, 0, path[ans]));
res = min(res, max(DFS(A, 0, path[ans + 1]), DFS(B, 0, path[ans + 1])));
cout << res;
}
Compilation message
torrent.cpp: In function 'int DFS(int, int, std::pair<int, int>)':
torrent.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < res.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
289 ms |
20992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |