codeforces 219D D. Choosing Capital for Treeland(树形dp)

题目连接:

codeforces 219D


题目大意:

给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。


题目分析:

  • 定义dp[u]为以u为根的子树中要使根都可达,需要调换方向的边的条数。
  • 定义dir[v]记录点v到父亲节点的边的方向。
  • 然后就是将每个点提成根的操作了。
  • dp[u]换成新的定义,以u为根的到达整棵树需要调整的边的条数。
  • dp[u] = dp[p] + dir[u]?1:-1
  • 详情见代码,这道题在D题当中算相当水的了。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAX 200007

using namespace std;

int n,s,t;
vector<int> e[MAX];
vector<int> f[MAX];
int dp[MAX],dir[MAX];

void add ( int u , int v )
{
    e[u].push_back ( v );
    f[u].push_back ( 0 );
    e[v].push_back ( u );
    f[v].push_back ( 1 );
}

void dfs ( int u , int p )
{
    dp[u] = 0;
    for ( int i = 0 ; i < e[u].size() ; i++ )
    {
        int v = e[u][i];
        if ( v == p ) continue;
        dir[v] = f[u][i];
        dfs ( v , u );
        dp[u] += dp[v] + dir[v];
    }
}

void solve ( int u , int p )
{
    for ( int i = 0 ; i < e[u].size() ; i++ )
    {
        int v = e[u][i];
        if ( v == p ) continue;
        dp[v] = dp[u] + (dir[v]?-1:1);
        solve ( v , u );
    }
}

int main ( )
{
    while ( ~scanf ( "%d" , &n ))
    {
        for ( int i = 0 ; i < MAX ; i++ )
        {
            e[i].clear();
            f[i].clear();
        }
        for ( int i = 1 ; i < n ; i++ )
        {
            scanf ( "%d%d" , &s , &t );
            add ( s , t );
        }
        dfs ( 1 , -1 );
        solve ( 1 , -1 );
        int ans = 1<<29;
        for ( int i = 1 ; i <= n ; i++ )
            ans = min ( ans , dp[i] );
        printf ( "%d\n" , ans );
        vector<int> pp;
        for ( int i = 1 ; i <= n ; i++ )
            if ( dp[i] == ans )
                pp.push_back ( i );
        sort ( pp.begin() , pp.end());
        for ( int i = 0 ; i < pp.size(); i++ )
            printf ( "%d " , pp[i] );
        puts ("");
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-10 22:13:55

codeforces 219D D. Choosing Capital for Treeland(树形dp)的相关文章

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

题目链接: http://codeforces.com/contest/219/problem/D 题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)如果有多个这样的点,则升序输出所有 思路: 看了三篇博客,挺好的 http://blog.csdn.net/chl_3205/article/details/9284747 http://m.blog.csdn.net/qq_32570675/article/d

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

Codeforces 219D. Choosing Capital for Treeland (树dp)

题目链接:http://codeforces.com/contest/219/problem/D 树dp 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio&

树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

题目传送门 1 /* 2 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 3 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计算dp[v], 4 此时可以从上个节点的信息递推出来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using name

(纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Over

Choosing Capital for Treeland CodeForces - 219D (树形DP)

传送门 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

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

题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正向边权值为0,反向边权值为1,那样就是各个点出发到其他点经过边所需的最少权值和. 然后对于每个点,分两个部分考虑:以这个点为根的子树.这个点往上走的部分: dp[0][u]表示以u点作为首都且以u点为根的子树部分所需反转边的数量,容易知道就等于子树内边权和 dp[1][u]表示以u点作为首都且u点向

Choosing Capital for Treeland codeforce 219-D

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