zoukankan      html  css  js  c++  java
  • CodeForces 219D 树形DP

    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. 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



    题意:

    给出N个点,其中有N-1条有向边,边的方向可以改变,问最少改变多少条边可以从某一个点到达任意一个点,同时求出这些点。

    代码:

     1 /*
     2 要改变的边的权值为1,不需要改变的边的权值为0,两次dfs,第一次算出以1点为根节点到所有点要改变的边数,第二次以1为根节点向下遍历节点
     3 算出每一个点到达所有点要改变的边数,dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一点的值是他父节点
     4 的值减去他以前的值再考虑他与父节点之间的边的方向。
     5 */
     6 #include<iostream>
     7 #include<string>
     8 #include<cstdio>
     9 #include<cmath>
    10 #include<cstring>
    11 #include<algorithm>
    12 #include<vector>
    13 #include<iomanip>
    14 #include<queue>
    15 #include<stack>
    16 using namespace std;
    17 int n,lne;
    18 int dp[200010];
    19 int head[200010];
    20 int city[200010];
    21 struct node
    22 {
    23     int to,next,val;
    24 }tree[400010];
    25 void add(int a,int b)
    26 {
    27     tree[lne].to=b;
    28     tree[lne].val=0;
    29     tree[lne].next=head[a];
    30     head[a]=lne++;
    31     tree[lne].to=a;
    32     tree[lne].val=1;
    33     tree[lne].next=head[b];
    34     head[b]=lne++;
    35 }
    36 void dfs1(int root,int pre)
    37 {
    38     for(int i=head[root];i!=-1;i=tree[i].next)
    39     {
    40         int son=tree[i].to;
    41         if(son==pre)
    42         continue;
    43         dfs1(son,root);
    44         dp[root]+=dp[son]+tree[i].val;
    45     }
    46 }
    47 void dfs2(int root,int pre)
    48 {
    49     for(int i=head[root];i!=-1;i=tree[i].next)
    50     {
    51         int son=tree[i].to;
    52         if(son==pre)
    53         continue;
    54         dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1);
    55         dfs2(son,root);
    56     }
    57 }
    58 int main()
    59 {
    60     int a,b;
    61     while(scanf("%d",&n)!=EOF)
    62     {
    63         lne=0;
    64         memset(head,-1,sizeof(head));
    65         for(int i=0;i<n-1;i++)
    66         {
    67             scanf("%d%d",&a,&b);
    68             add(a,b);
    69         }
    70         int k=0,sum=10000000;
    71         memset(dp,0,sizeof(dp));
    72         dfs1(1,0);
    73         dfs2(1,0);
    74         for(int i=1;i<=n;i++)
    75         {
    76             if(dp[i]<sum)
    77             {
    78                 memset(city,0,sizeof(city));
    79                 k=0;
    80                 city[k++]=i;
    81                 sum=dp[i];
    82             }
    83             else if(dp[i]==sum)
    84             city[k++]=i;
    85         }
    86         printf("%d
    ",sum);
    87         for(int i=0;i<k;i++)
    88         {
    89             if(i==k-1)
    90             printf("%d
    ",city[i]);
    91             else printf("%d ",city[i]);
    92         }
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    Request功能
    Request继承体系
    HTTP协议:请求消息的数据格式---Request
    HTTP协议---HttpServlet
    hdu 1575 矩阵连乘2
    hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)
    矩阵连乘
    MongoDB(六):选择字段、限制记录数、排序记录
    MongoDB(五):更新文档、删除文档
    爬虫(八):文件处理
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5786233.html
Copyright © 2011-2022 走看看