이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
}
reverse(path.begin(), path.end());
// 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;
}
컴파일 시 표준 에러 (stderr) 메시지
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |