Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree(DP)

题目链接:http://codeforces.com/contest/1065/problem/F

 

只有两种情况,一种是访问一个子树,最终能回到当前节点,另一种是访问一个子树,最后没法回到当前节点

f[i]表示离i最近的叶子节点到i的深度

dp[i]表示以i为根,并且回到i,能够访问的叶子数目

ans[i]表示以i为根,能访问的的最大的节点数

然后dfs转移即可

 

代码:

#include
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair pii;
const int MAXN=1e6+5;
vector E[MAXN];
int n,k,dp[MAXN],f[MAXN],ans[MAXN];
void dfs(int now,int d=0)
{
	if(E[now].size()==0)
	{
		f[now]=0;
		dp[now]=1;
		return ;
	}
	int mi=1e9,mx=0;
	for(const int &v:E[now])
	{
		dfs(v,d+1);
		if(f[v]

 

你可能感兴趣的:(codeforces)