zoukankan      html  css  js  c++  java
  • Codeforces 131D. Subway 寻找环树的最短路径

    D. Subway
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used to move in both directions. Between each pair of stations there is no more than one passage.

    Berland mathematicians have recently proved a theorem that states that any classic scheme has a ringroad. There can be only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any two neighbouring ones are linked by a passage) and this cycle doesn't contain any station more than once.

    This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in one passage from the ringroad". The Internet soon got filled with applications that promised to count the distance from the station to the ringroad (send a text message to a short number...).

    The Berland government decided to put an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme.

    Input

    The first line contains an integer n (3 ≤ n ≤ 3000), n is the number of stations (and trains at the same time) in the subway scheme. Thenn lines contain descriptions of the trains, one per line. Each line contains a pair of integers xi, yi (1 ≤ xi, yi ≤ n) and represents the presence of a passage from station xi to station yi. The stations are numbered from 1 to n in an arbitrary order. It is guaranteed that xi ≠ yi and that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.

    Output

    Print n numbers. Separate the numbers by spaces, the i-th one should be equal to the distance of the i-th station from the ringroad. For the ringroad stations print number 0.

    Sample test(s)
    input
    4
    1 3
    4 3
    4 2
    1 2
    
    output
    0 0 0 0 
    input
    6
    1 2
    3 4
    6 4
    2 3
    1 3
    3 5
    
    output
    0 0 0 1 1 2 

    图只有一个环,找到每个点距离环的最短距离。


    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    bool a[4111][4111];
    int n;
    bool v[4111]={0};
    int p[4111]={0};
    int cnt=0;
    int ans[4111]={0};
    
    bool dfs_find(int i,int lt)
    {
        v[i]=true;
        for (int j=1;j<=n;j++)
        {
            if (a[i][j]>0&<!=j&&i!=j)
            {
                if (v[j])
                {
                    p[cnt++]=j;
                    return true;
                }
                else
                {
                    p[cnt++]=j;
                    if (dfs_find(j,i))
                    {
                        return true;
                    }
                    cnt--;
                }
            }
        }
        return false;
    }
    
    void dfs(int i)
    {
        for (int j=1;j<=n;j++)
        {
            if (i!=j&&a[i][j]>0&&ans[j]==-1)
            {
                ans[j]=ans[i]+1;
                dfs(j);
            }
        }
    }
    
    int main()
    {
        memset(a,0,sizeof(a));
        memset(ans,-1,sizeof(ans));
        cin>>n;
        for (int i=1;i<=n;i++)
        {
            int x,y;
            cin>>x>>y;
            a[x][y]=a[y][x]=1;
        }
        p[cnt++]=1;
        dfs_find(1,0);
    
        ans[p[cnt-1]]=0;
        for (int i=cnt-2;i>=0;i--)
        {
            if (p[i]==p[cnt-1]) break;
            ans[p[i]]=0;
        }
    
        for (int i=1;i<=n;i++)
        {
            if (ans[i]==0)
            {
                dfs(i);
            }
        }
    
        for (int i=1;i<=n;i++)
        {
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    
        return 0;
    }





  • 相关阅读:
    《vue.js2.0从入门到放弃》学习之路
    动画统计图
    超简单的走马灯效果
    关于css那些常用却有点记不住的属性
    圣杯布局跟双飞翼布局
    最简单的http服务器(C#)
    sql union用法和sql union all用法,sql union效率
    存储过程函数中如何定义表变量,删除表变量内容
    C# 通过分析netstat an所得信息 查看本机所监听的端口 及判断某端口是否可用
    Microsoft .NET Framework 各版可再发行组件包
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038454.html
Copyright © 2011-2022 走看看