이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=300100;
int n,f[N],A,B,fa[N];
vector<int>ss,v[N],d;
void solve(int x,int y,int t)
{
for(int i:v[x])
if(i!=y&&i!=t)solve(i,x,t);
ss.clear();f[x]=0;
for(int i:v[x])
if(i!=y&&i!=t)ss.push_back(f[i]);
sort(ss.begin(),ss.end());
reverse(ss.begin(),ss.end());
for(int i=0;i<ss.size();i++)
f[x]=max(f[x],i+1+ss[i]);
}
void dfs(int x)
{
for(int i:v[x])
if(i!=fa[x])fa[i]=x,dfs(i);
}
signed main()
{
scanf("%lld%lld%lld",&n,&A,&B);
for(int i=1,x,y;i<n;i++)
scanf("%lld%lld",&x,&y),
v[x].push_back(y),v[y].push_back(x);
dfs(A);
int now=B;
while(now!=A)
d.push_back(now),now=fa[now];
d.push_back(A);
reverse(d.begin(),d.end());
int l=0,r=d.size()-2,mid,ans=1e18;
while(l<=r)
{
mid=(l+r)>>1;
solve(A,0,d[mid+1]),solve(B,0,d[mid]);
// cout<<d[mid+1]<<' '<<d[mid]<<endl;
// cout<<"ANS="<<
ans=min(ans,max(f[A],f[B]));
if(f[A]>f[B])r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
}
컴파일 시 표준 에러 (stderr) 메시지
torrent.cpp: In function 'void solve(long long int, long long int, long long int)':
torrent.cpp:16:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i=0;i<ss.size();i++)
| ~^~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%lld%lld%lld",&n,&A,&B);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%lld%lld",&x,&y),
| ~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |