zoukankan      html  css  js  c++  java
  • 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 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
     21 2 3
     
    题解:将方向转化为边的权值,正向边权值为0,反向边权值为1.以1为根DFS,得到1到其它所有点的总花费。重点是第二次DFS:
           
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=2e5+5;
     5 
     6 struct node{
     7     int to,next,va;
     8 }e[2*maxn];
     9 
    10 int n,tot;
    11 int dp[maxn],head[maxn];
    12 
    13 void Inite(){
    14     tot=0;
    15     memset(head,-1,sizeof(head));
    16 }
    17 
    18 void addedge(int u,int v,int w){
    19     e[tot].to=v;
    20     e[tot].va=w;
    21     e[tot].next=head[u];
    22     head[u]=tot++;
    23 }
    24 
    25 void DFS1(int pa,int u){
    26     for(int i=head[u];i!=-1;i=e[i].next){
    27         int v=e[i].to;
    28         if(pa==v) continue;
    29         DFS1(u,v);
    30         dp[u]+=dp[v]+e[i].va;
    31     }
    32 }
    33 
    34 void DFS2(int pa,int u){
    35     for(int i=head[u];i!=-1;i=e[i].next){
    36         int v=e[i].to;
    37         if(pa==v) continue;
    38         dp[v]+=(dp[u]-dp[v])+((e[i].va)?-1:1);
    39         DFS2(u,v);
    40     }
    41 }
    42 
    43 int main()
    44 {   Inite();
    45     memset(dp,0,sizeof(dp));
    46 
    47     scanf("%d",&n);
    48     for(int i=2;i<=n;i++){
    49         int u,v;
    50         scanf("%d%d",&u,&v);
    51         addedge(u,v,0);
    52         addedge(v,u,1);
    53     }
    54     
    55     DFS1(0,1);
    56     DFS2(0,1);
    57     
    58     int temp=1e9;
    59     vector<int> q;
    60     for(int i=1;i<=n;i++) if(dp[i]<temp) temp=dp[i];
    61     for(int i=1;i<=n;i++) if(dp[i]==temp) q.push_back(i);
    62     printf("%d
    ",temp);
    63     for(int i=0;i<q.size();i++) printf("%d%c",q[i],(i==(q.size()-1))?'
    ':' ');
    64     
    65 }
    
    
  • 相关阅读:
    词汇表处理脚本
    jLowNote又,我为什么要说又,有bug
    于是按照贴吧某同学的指教,把imageViewer里那个愚蠢的语句改了
    捉到Bug一只,jLowNote里的
    高产赛母猪
    我超喜欢Nimbus风格的!
    专注写记事本三十年
    秒秒钟食言
    别再打了,Java和Python,你们其实都是C
    电话本写完了,发个1.0吧
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7765972.html
Copyright © 2011-2022 走看看