zoukankan      html  css  js  c++  java
  • Codeforces Round #135 (Div. 2) 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.

    Sample test(s)
    input
    3
    2 1
    2 3
    
    output
    0
    2 
    
    input
    4
    1 4
    2 4
    3 4
    
    output
    2
    1 2 3 
    
    题意:给你n个点所组成的树,每条边的方向是确定的,让你确定一个源点,使得为了使这个点能到达其他所有点改变的路径方向数最少。
    思路:同样是用两次dfs搜一下就行了,第一次记录子树范围内的,第二次从父亲节点转移。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 0x7fffffff
    #define maxn 200005
    int first[maxn];
    struct node{
        int to,qidian,next;
    }e[2*maxn];
    int num[maxn],vis[maxn];
    int minx;
    
    void dfs(int u)
    {
        int i,j,v;
        int flag=0;
        vis[u]=1;
        for(i=first[u];i!=-1;i=e[i].next){
            v=e[i].to;
            if(vis[v])continue;
            flag=1;
            dfs(v);
            if(e[i].qidian==u){
                num[u]+=num[v];
            }
            else{
                num[u]+=num[v]+1;
            }
        }
        if(flag==0){
            num[i]=0;return;
        }
    
    }
    
    void dfs1(int u)
    {
        int i,j,v;
        vis[u]=1;
        for(i=first[u];i!=-1;i=e[i].next){
            v=e[i].to;
            if(vis[v])continue;
            if(e[i].qidian==u){
                num[v]=num[u]+1;
            }
            else{
                num[v]=num[u]-1;
            }
            minx=min(minx,num[v]);
            dfs1(v);
        }
    }
    
    
    int main()
    {
        int n,m,i,j,T,c,d,tot,flag1;
        while(scanf("%d",&n)!=EOF)
        {
            tot=0;
            memset(first,-1,sizeof(first));
            for(i=1;i<=n-1;i++){
                scanf("%d%d",&c,&d);
                tot++;
                e[tot].next=first[c];e[tot].to=d;e[tot].qidian=c;
                first[c]=tot;
    
                tot++;
                e[tot].next=first[d];e[tot].to=c;e[tot].qidian=c;
                first[d]=tot;
    
            }
            minx=inf;
            memset(vis,0,sizeof(vis));
            memset(num,0,sizeof(num));
            dfs(1);
            memset(vis,0,sizeof(vis));
            minx=min(minx,num[1]);
            dfs1(1);
            printf("%d
    ",minx);
            flag1=1;
            for(i=1;i<=n;i++){
                if(num[i]==minx){
                    if(flag1==1){
                        flag1=0;
                        printf("%d",i);
                    }
                    else{
                        printf(" %d",i);
                    }
                }
            }
            printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    elasticsearch ——id字段说明,内部是_uid
    企业安全建设之搭建开源SIEM平台(上)
    江西鹰潭、江西移动与华为战略合作:共推物联网——物联网的世界要到来了
    Luke 5—— 可视化 Lucene 索引查看工具,可以查看ES的索引
    Apache Flink vs Apache Spark——感觉二者是互相抄袭啊 看谁的好就抄过来 Flink支持在runtime中的有环数据流,这样表示机器学习算法更有效而且更有效率
    转:shell比较两个字符串是否相等
    UNIX 缩写风格
    转:.Net程序员学习Linux最简单的方法
    asp.net插入sql server 中文乱码问题解决方案
    asp.net将object或string转为int
  • 原文地址:https://www.cnblogs.com/herumw/p/9464632.html
Copyright © 2011-2022 走看看