首页 > 其他分享 >树形DP (cf 219D Choosing Capital for Treeland)

树形DP (cf 219D Choosing Capital for Treeland)

时间:2023-02-07 17:00:52浏览次数:41  
标签:219D Treeland int cf edge capital roads include dp


题意翻译

题目描述

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 (cf 219D Choosing Capital for Treeland)_权值

 

化简一下: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;
}

 

标签:219D,Treeland,int,cf,edge,capital,roads,include,dp
From: https://blog.51cto.com/u_14932227/6042482

相关文章

  • CF1787I Treasure Hunt 题解
    题目链接题意描述:定义一个序列的权值为一段前缀与一段子段,满足要么前缀与子段不交,要么完全包含的和的最大值,给定一个序列\(a\),求\(a\)的所有子区间的权值和\(n\le1......
  • 每日一道思维题——CF1742F - Smaller
    题意:存在字符串s,t(初始使都为"a"),有1,2两种操作方式1.将s后面+d个字符串x2.将t后面+d个字符串x操作完成后,询问是否可以改变字符串s,t中字符顺序,使得s字典序小于t若可,输......
  • CF253D Table with Letters - 2 优美枚举
    题目描述VasyahasrecentlystartedtolearnEnglish.NowheneedstorememberhowtowriteEnglishletters.Heisn'tsureaboutsomeofthem,sohedecidedtot......
  • 标志位(CF、ZF、SF、OF、PF)
    CF(carryflag):进/借位标志位无符号数进行运算,若加法时最高位(D7或D15)产生进位或减法时最低位产生借位,CF=1,否则CF=0。ZF(zeroflag):零标志位运算结果的每一位都为0,ZF......
  • CF1137F Matches Are Not a Child's Play 题解
    以最后被删去的点为根,这样子不会存在从父亲然后删掉某个点,儿子的删除顺序一定比父亲前。记每个点子树中的最大值为\(f_x\),那么一个点的排名,首先就需要加上\(<f_x\)的所......
  • CF1785
    A略,具体见C。B这题卡了我一会,写起来又花了挺长一段时间的,水平还是不够!贪心一下,优先取贡献为\(2\)的,其次取贡献为\(1\)的,看上去就很对。Code:#include<bits/stdc......
  • 记 CF1778 - 成功上紫 -
    打了一个寒假终于在寒假末尾(小号)打上紫了~(大号好几年没打过比赛了)其实之前还有一次很有希望的一场,结果出了个错题unrated了……2D是一个期望题,本来没什么信心的结果......
  • CF884D 题解
    题目传送门题目分析开始还真没看出来这题跟\(\text{P1090}\)合并果子的关系。其实只要逆向思考,把拆分看成效果一样的合并就可以了。而与合并果子不同的是,在这题当中......
  • CF1778E The Tree Has Fallen!
    啊哈!线性基!当然重点是如何处理换根部分,顺带记录一下有关线性基的东西。\(n\)个数的线性基:构造\(O(n\loga_i)\),合并\(O(\log^2a_i)\),查询\(O(\loga_i)\)。以\(1......
  • 论文阅读: CCF A 2021 PROGRAML:用于数据流分析和编译器优化的基于图的程序表示 (PMLR
       Motivation:  编译器实现是一项复杂而昂贵的活动。出于这个原因,人们对使用机器学习来自动化各种编译器任务产生了极大的兴趣,大多数工作都将注意力限制在选择编......