【CodeForces 219D】Choosing Capital for Treeland(树形dp)

本文探讨在Treeland国家,由n个城市组成的网络中,如何选择首都以最小化道路逆向次数的问题。通过两次深度优先搜索(DFS),文章详细介绍了如何确定每个城市作为首都时所需的逆向道路数量,从而找出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.

The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.

Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.

Input

The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ nsi ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.

Output

In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.

Examples

Input

3
2 1
2 3

Output

0
2 

Input

4
1 4
2 4
3 4

Output

2
1 2 3 

思路:

 

思路:

任选一个点作为根节点,dfs搜一遍,找到根节点需要转方向的边有多少,然后再来dfs从上往下推,每到一层就推出这一层的结果。例如根节点是1号它的子节点之一是2号,那么1号和2号在2这可子树上需要逆向的边数是相同的,因为这一边的信息是2传给1的。再看1号结点连的其他子树(除2号这一支之外),当以2号为根节点时,2号会扫到1号,然后会扫1号的子节点,这样1号和2号扫1号的子节点是相同的操作,因此 两者得到的结果是相同的。这样来说两者数量的差别就体现在两者之间的边上。如果1扫到2时正向的,那么2到1就是逆向的,2的逆向边比1多,所以dp[2]=dp[1]+1;反之会比1少一条。因此我们可以借助父节点与子节点之间的关系,从选定的根节点一直推到叶子节点。 需要用到两次dfs

ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
const int MAXN=2e6+100;
struct node{
	int x,nex,w;
}side[MAXN*2];//加双向边一定记得开两倍的数组 
int n,u,v;
int head[MAXN],cnt=0;
int num[MAXN],dp[MAXN];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
	memset(dp,0,sizeof(dp));
	memset(num,0,sizeof(num));
}
void add(int x,int y,int w)
{
	side[cnt].x=y;
	side[cnt].nex=head[x];
	side[cnt].w=w;
	head[x]=cnt++;
	
}

void dfs(int x,int f)
{
	
	for(int i=head[x];i!=-1;i=side[i].nex)
	{
		int ty=side[i].x;
		if(ty==f)
		continue;
		dfs(ty,x);
		num[x]+=num[ty];
		if(!side[i].w)
		num[x]++;
	}
	dp[x]=num[x];
}
void df(int x,int f)
{
	for(int i=head[x];i!=-1;i=side[i].nex)
	{
		int ty=side[i].x;
		if(f==ty)//要判断这一步否则会陷入死循环 
		continue; 
		if(!side[i].w)
		dp[ty]=dp[x]-1;
		else
		{
			dp[ty]=dp[x]+1;
		}
		df(ty,x);
	}
}
int main()
{
	init();
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v,1);//1表示边原来是正向的(标准方向) 
		add(v,u,0);// 0表示不是标准方向 
	}
		dfs(1,-1);
		df(1,-1);
	int ans=dp[1];
	int tot=1,ot[MAXN];
	ot[0]=1;
	for(int i=2;i<=n;i++)
	{
		if(ans>dp[i])
		{
			ans=dp[i];
			ot[0]=i;
			tot=1;
		}
		else if(ans==dp[i])
		{
			ot[tot++]=i;
		}
	}
	printf("%d\n",ans);
	for(int i=0;i<tot;i++)
	{
		printf("%d",ot[i]);
		if(i==tot-1)
		printf("\n");
		else
		printf(" ");
	}
	
	return 0;
}

 

看了别人的思路感觉这样想也挺清晰的,感觉这种转换的思想挺好的,以后在题目中可能会用到这种思想,借鉴一下

思路:

把边的方向化为权值,正向为1,逆向为0。

问题转化为找哪些点的在遍历全图后总权值最大。

这就是树形DP了,考虑每个节点,它可以从子树收获价值,也可以从父亲收获。所以dfs两遍,一边把子树的价值存到dps[i]里,再一遍把父亲的价值存到dpf[i]里。ans[i] = dps[i] + dpf[i]。

原文链接:https://blog.youkuaiyun.com/angon823/article/details/52316220 

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值