Codeforces 813C The Tag Game 贪心

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 813C The Tag Game 贪心相关的知识,希望对你有一定的参考价值。

#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 贪心的主要内容,如果未能解决你的问题,请参考以下文章

题解 CF813C The Tag Game

codeforces 502 g The Tree

Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

codeforces 682C Alyona and the Tree DFS

codeforces 776E The Holmes Children

Codeforces 839B - Game of the Rows