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;
}

  

时间: 2024-10-15 06:45:25

Codeforces 813C The Tag Game 贪心的相关文章

CodeForces - 813C The Tag Game(拉格朗日乘数法,限制条件求最值)

[传送门]http://codeforces.com/problemset/problem/813/C [题意]给定整数a,b,c,s,求使得  xa yb zc值最大的实数 x,y,z , 其中x + y + z <= s. (1?≤?S?≤?103  , 0?≤?a,?b,?c?≤?103) [题解]设P(x,y,z ) = xa yb zc,则P(x,y,z)是递增的,要使 函数值尽可能地大,那么必取 x + y + z = s 问题转化成:已知限定条件  x + y + z = s, 求

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1

codeforces 343C Read Time 二分 + 贪心

http://codeforces.com/problemset/problem/343/C 题意: 有一些磁头,给出了起始的位置,给出了一些磁盘中需要访问的地点.求这些磁头移动的最小的次数. 思路: 二分找出满足要求的最小的时间,对于当前尝试的时间进行贪心判断是否可用.对于贪心,因为每一个磁头都需要读到还没有人读过的最左边的地方.如果这个都不能满足,那么这个时间是不可以的. 在这基础上,对于同样的这么多时间,尽可能的让这个磁头读到能读到的最右边,这样就可以将还没有读过的地方尽可能的往右推. 如

Codeforces 402D Upgrading Array:贪心 + 数学

题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中p是s的最小质因子: f(1) = 0 f(s) = f(s/p) + 1 (p不是坏质数) f(s) = f(s/p) - 1 (p是坏质数) 你可以任意次数地进行操作:给a[1 to i]的每个数都除以gcd(a[1 to i]). 问你 ∑ f(a[i)最大为多少. 题解: 函数f(s)的实际

Codeforces 1119E Pavel and Triangles (贪心)

Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal to powers of two. He has \(a_0\) sticks of length \(2^0=1\), \(a1\) sticks of length \(2^1=2\), ..., \(a_{n?1}\) sticks of length \(2^{n?1}\). Pavel wa