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;
    }
    


  • 相关阅读:
    一些tips
    微信小程序之后端处理
    微信小程序之前端代码篇
    微信小程序踩坑之前端问题处理篇
    Vue组件封装之一键复制文本到剪贴板
    读别人的代码之bug的发现
    解析webpack插件html-webpack-plugin
    数组去重方法整理
    如何理解EventLoop--浏览器篇
    axios和vue-axios的关系
  • 原文地址:https://www.cnblogs.com/herumw/p/9464632.html
Copyright © 2011-2022 走看看