树形dp 219D Choosing Capital for Treeland

http://codeforces.com/problemset/problem/219/D

题意:

可以这么理解为—— 一些城市的道路之间是有向边,如果沿着路的方向来走花费的价值为 0,如果逆向行走花费的价值为 1,选择一些点为首都,使其到其他点所花费的价值最小

思路:

树形dp

运用两次深搜,第一次深搜求出点i到以其自身为根节点的子树上其他点总共需要花费的价值,dp[ i ] 记录。

第二次深搜,根据第一次dp的结果,利用父节点的信息来更新子节点,求出点i到整个图中其他任意点所花费的价值,dp[ i ] 记录。

如果root指向son   dp[son] = dp[root] + 1;

如果son指向root   dp[root] = dp[son] + 1;

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int N =2e5+5;
struct node
{
    int v,w;
};
vector<node> g[N];
bool flag[N];
int dp[N];
queue<int> q;
void dfs(int root)
{
    flag[root] = true;
    int i;
    for(i= 0; i < (int)g[root].size(); i++)
    {
        int son = g[root][i].v;
        if(flag[son]) continue;
        dfs(son);
        if(g[root][i].w == 0)
        {
            dp[root] += dp[son] + 1;
        }
        else
        {
            dp[root] += dp[son];
        }
    }
}
void DFS(int root)
{
    flag[root] = true ;
    int i;
    for(i = 0; i < (int) g[root].size(); i++)
    {
        int son = g[root][i].v;
        if(flag[son]) continue;
        if(g[root][i].w == 0)
        {
            dp[son] = dp[root] - 1; // 反向
        }
        else
        {
            dp[son] = dp[root] + 1; // 正向
        }
        DFS(son);
    }
}
int main()
{
    int n,i,u,v;
    while(~scanf("%d",&n))
    {
        for(i = 1; i <= n; i++)
        {
            flag[i] = false;
            g[i].clear();
            dp[i] = 0;
        }
        node temp;
        for(i = 1; i < n; i++)
        {
            scanf("%d%d",&u,&v);
            temp.v = v;
            temp.w = 1;
            g[u].push_back(temp);
            temp.v = u;
            temp.w = 0;
            g[v].push_back(temp);
        }
        dfs(1);
        memset(flag,false,sizeof(flag));
        DFS(1);
        while(!q.empty())
        {
            q.pop();
        }
        int Min = N;
        for(i = 1; i <= n; i++)
        {
            if(dp[i] < Min)
            {
                Min = dp[i];
                while(!q.empty())
                {
                    q.pop();
                }
                q.push(i);
                continue;
            }
            if(dp[i] == Min)
            {
                q.push(i);
            }
        }
        printf("%d\n",Min);
        printf("%d",q.front());
        q.pop();
        while(!q.empty())
        {
            printf(" %d",q.front());
            q.pop();
        }
        puts("");
    }
    return 0;
}


ollecting alphalens Using cached alphalens-0.4.0.tar.gz (24.0 MB) Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [26 lines of output] /tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/versioneer.py:564: SyntaxWarning: invalid escape sequence '\s' mo = re.search(r'=\s*"(.*)"', line) Traceback (most recent call last): File "<string>", line 2, in <module> exec(compile(''' ~~~~^^^^^^^^^^^^ # This is <pip-setuptools-caller> -- a caller that pip uses to run setup.py ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<32 lines>... exec(compile(setup_py_code, filename, "exec")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ''' % ('/tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/setup.py',), "<pip-setuptools-caller>", "exec")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<pip-setuptools-caller>", line 35, in <module> File "/tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/setup.py", line 35, in <module> version=versioneer.get_version(), ~~~~~~~~~~~~~~~~~~~~~~^^ File "/tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/versioneer.py", line 1480, in get_version return get_versions()["version"] ~~~~~~~~~~~~^^ File "/tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/versioneer.py", line 1412, in get_versions cfg = get_config_from_root(root) File "/tmp/pip-install-xgmte2ke/alphalens_c71a219d7adf4a65ae4a00db544ad00e/versioneer.py", line 342, in get_config_from_root parser = configparser.SafeConfigParser() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'configparser' has no attribute 'SafeConfigParser'. Did you mean: 'RawConfigParser'? [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. 如何解决
最新发布
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值