1. 程式人生 > >Codeforces 813C The Tag Game 貪心

Codeforces 813C The Tag Game 貪心

oid pac vector cin main sizeof pre void -1

#include <bits/stdc++.h>
using namespace std;
const int N=4e5+20;
const int inf=2e6;
int n,x,par[N],dep[N],dp[N],ans;//dp[i] ×óê÷iμ?ò??úμ?μ?×?′óé??è
//ìaòa:n???áμ?μ?ê÷,n<=2e5,Alice?ú?áμ?1,Bob?ú?áμ?x,??′??éò?ò??ˉμ??àáúμ??áμ??ò??2??ˉ.
//Aliceò??ˉμ?bob?ù?ú?áμ?Aliceê¤à?,Alice?????ì??ê¤bob??ííμ??áê?,?ê×üμ?2ù×÷êy?

//Alice???¨íùbob?ù?úμ?×?,Bob??íí?áê?,???üíùé??è??μ?ò?×ó?áμ?×?è?oó2??ˉ. 
//?áμ?1é??è?a0,x?áμ?é??è?ad,bob×??àíùé?×?(d-1)/2 ?úμ??3??ò??áμ?,?ú?aD?×óê÷?D????é??è×?′óμ?ò??úμ??′?é 
vector<int> e[N]; 
void dfs(int u,int fa,int d)
{
	dep[u]=dp[u]=d;
	par[u]=fa;//×??è 
	for(int i=0;i<e[u].size();i++)
	{
		int v=e[u][i];
		if(v==fa)	continue;
		dfs(v,u,d+1);
		dp[u]=max(dp[u],dp[v]);
	}	

}

int main()
{
	while(cin>>n>>x)
	{
		ans=0;
		for(int i=1;i<=n;i++)
			dp[i]=0,e[i].clear();
		memset(dep,0,sizeof(dep));
		int u,v;
		for(int i=1;i<=n-1;i++)
		{
			scanf("%d%d",&u,&v);
			e[u].push_back(v);
			e[v].push_back(u);
		}
		dfs(1,-1,0);
		ans=dp[x]*2;
		int D=(dep[x]-1)/2;
		while(D)//x×??à?òé?×?D′?,×?oóò??áμ????¨?aD???Dμ??3ò??? 
		{
			x=par[x];
			ans=max(ans,dp[x]*2);
			D--;
		}
		cout<<ans<<endl;
	}
	return 0;
}

  

Codeforces 813C The Tag Game 貪心