本文主要是介绍树形DP (cf 219D Choosing Capital for Treeland),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意翻译
题目描述
Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。
输入输出格式
输入格式
输入包含多个测试点。对于每个测试点,每个测试点的第一行为一个正整数n(2<=n<=2e5)。接下来n-1行,每行两个正整数ai,bi,表示城市a到城市b有一条单向通行的道路。输入以空格分隔,以EOF结尾。
输出格式
对于每个测试点,第一行输出k,第二行升序输出所有满足条件的首都的编号。
题目描述
The country Treeland consists of nn cities, some pairs of them are connected with unidirectional roads. Overall there are n-1n−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 aa is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city aa 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.
输入输出格式
输入格式:
The first input line contains integer nn ( 2<=n<=2·10^{5}2<=n<=2⋅105 ) — the number of cities in Treeland. Next n-1n−1lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers s_{i},t_{i}si,ti ( $ 1<=s_{i},t_{i}<=n; s_{i}≠t_{i} $ ) — the numbers of cities, connected by that road. The ii -th road is oriented from city s_{i}si to city t_{i}ti . You can consider cities in Treeland indexed from 1 to nn .
输出格式:
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.
输入输出样例
输入样例#1: 复制
3
2 1
2 3
输出样例#1: 复制
0
2
输入样例#2: 复制
4
1 4
2 4
3 4
输出样例#2: 复制
2
1 2 3
算法分析
题意:
有一棵有n个节点的数,有n-1条有向边,每条边的方向可以被翻转,但是代价为1,问你应该选取哪些点为根, 使从根到达所有点的代价最下。先输出最小代价,然后输出所有的可取点。
分析:
从每个点都可以到达与他相邻的点,只不过走的方向不同代价就不同 所以我们可以将题目给出的正向边的权值赋值为0,而他的反向边赋值为1,那么题目就转换成了求每个点都其他点的权值之和的最小值了。
关于上面这个问题就可以用树型DP的思想了:
dfs1()求出每个点到以他为根子树的所有的节点的权值之和
设dp[i]]为从节点i出发,到达他所有的子节点所需要修改的最少边数。
dp[u] += dp[v] + w(u,v)
设w(u,v)为他父亲到节点i的方向,正向为0,反向为1。
dfs2()从上往下更新一遍即可:
改变dp[i]的定义为节点i到整棵树所需要修改的最少边数。
公式好好画图理解
化简一下:dp[v]=dp[u]+e[i].va?-1:1;
代码实现
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=200010;
struct node
{int v;///终端点int next;///下一条同样起点的边号int w;///权值
}edge[N*2];///无向边,2倍
int head[N];///head[u]=i表示以u为起点的所有边中的第一条边是 i号边
int tot; ///总边数
int minn;
void add(int u,int v,int w)
{edge[tot].v=v;edge[tot].w=w;edge[tot].next=head[u];head[u]=tot++;
}
int n;
int dp[N];
int dfs(int u,int fa)
{for(int i=head[u];i!=-1;i=edge[i].next){int v= edge[i].v;if(fa==v) continue; ///如果下一个相邻节点就是父节点,则证明到底层了,开始递归父节点的兄弟节点dfs(v,u);dp[u]+=edge[i].w;dp[u]+=dp[v];}}
int dfs2(int u,int fa)
{for(int i=head[u];i!=-1;i=edge[i].next){int v = edge[i].v;if(v == fa) continue;if(edge[i].w == 0) dp[v] = dp[u] + 1;else dp[v] = dp[u] - 1;if(dp[v] < minn) minn = dp[v];dfs2(v,u);}}
int main()
{scanf("%d",&n);memset(head,-1,sizeof(head));memset(dp,0,sizeof(dp));tot=0;for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);add(u,v,0);add(v,u,1);}dfs(1,-1);minn=dp[1];dfs2(1,-1);printf("%d\n",minn);for(int i = 1;i <= n; i++){if(minn == dp[i]){printf("%d ",i);}}return 0;
}
这篇关于树形DP (cf 219D Choosing Capital for Treeland)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!