답안 #696089

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696089 2023-02-05T11:44:12 Z vjudge1 Torrent (COI16_torrent) C++14
100 / 100
411 ms 27536 KB
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
vector<int>g[300100];
int st[300100],to,sta[300010],top;
bool fl=0;
void df(int x,int f){
	st[++to]=x;
	if(x==b){
		top=to;
		for(int i=1;i<=top;i++)sta[i]=st[i];
	}
	for(int v:g[x])if(v!=f)df(v,x);
	to--;
}
bool vis[301000];
int dp[301000];
void dfs(int x,int f){
	for(int v:g[x])if(!vis[v]&&v!=f)dfs(v,x);
	vector<int>ok;
	for(int v:g[x])if(!vis[v]&&v!=f)ok.push_back(dp[v]);
	sort(ok.begin(),ok.end());reverse(ok.begin(),ok.end());
	dp[x]=0;
	for(int i=0;i<(int)ok.size();i++)dp[x]=max(dp[x],i+1+ok[i]);
}
bool ck(int x){
	vis[sta[x+1]]=1,dfs(a,0),vis[sta[x+1]]=0;
	vis[sta[x]]=1,dfs(b,0),vis[sta[x]]=0;
	bool fl=(dp[a]<=dp[b]);
	return fl;
}
int main(){
	scanf("%d%d%d",&n,&a,&b);
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		g[u].push_back(v),g[v].push_back(u);
	}
	df(a,0);
	int l=1,r=top-1,x=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(ck(mid))x=mid,l=mid+1;
		else r=mid-1;
	}
	int ans=n+1;
	if(x)ck(x),ans=min(ans,dp[b]);
	if(x<top-1)ck(++x),ans=min(ans,dp[a]); 
	return printf("%d",ans),0;
}

Compilation message

torrent.cpp: In function 'int main()':
torrent.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |  scanf("%d%d%d",&n,&a,&b);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
torrent.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |   scanf("%d%d",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7360 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 411 ms 23712 KB Output is correct
2 Correct 400 ms 25820 KB Output is correct
3 Correct 391 ms 27292 KB Output is correct
4 Correct 397 ms 26424 KB Output is correct
5 Correct 390 ms 23900 KB Output is correct
6 Correct 406 ms 24552 KB Output is correct
7 Correct 360 ms 27536 KB Output is correct