이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define st first
#define nd second
#define ll int64_t
using namespace std;
const int SZ=3e5+7;
vector <int> node[SZ];
int dp[SZ],GetA[SZ],GetB[SZ],a,b,go(0);
void dfs(int v,int p,int z) {
if(node[v].size()==1 && v!=a && v!=b) return ;
vector <int> went;
for(auto x:node[v]) {
if(x==p || x==z) continue;
dfs(x,v,z);
went.emplace_back(dp[x]);
}
int mx(0);
sort(went.begin(),went.end(),greater<int>());
for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
dp[v]=mx;
return ;
}
int main(void) {
cin.tie(0)->ios::sync_with_stdio(false);
int n,res((int)1e9);
cin >> n >> a >> b;
for(int i=1,x,y;i<n;i++) {
cin >> x >> y;
node[x].emplace_back(y);
node[y].emplace_back(x);
}
queue <int> hx;
hx.push(a);
GetA[a]=a;
while(!hx.empty()) {
auto o=hx.front();
hx.pop();
for(auto x:node[o]) {
if(!GetA[x]) {
GetA[x]=o;
hx.push(x);
}
}
}
int you=b;
while(you!=a) {
GetB[++go]=you;
you=GetA[you];
}
int l=1,h=go;
while(l<=h) {
int mi=(l+h)>>1;
dfs(a,-1,GetB[mi]);
dfs(b,-1,GetA[GetB[mi]]);
res=min(res,max(dp[a],dp[b]));
if(dp[a]>dp[b]) {
l=mi+1;
}else h=mi-1;
}
cout << res;
}
컴파일 시 표준 에러 (stderr) 메시지
torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |