• Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs


    D. Subway

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

    Examples
    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 
    题意:给你一个无向图,只有一个环,求各个点到环的最短距离;
       dfs求环,bfs求距离;
     
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define inf 2000000001
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF )  return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    int huan[5010],jiedge,num;
    int vis[5010];
    struct is{int v,next;};
    is edge[9010];
    int head[5010];
    int ans[5010];
    void addedge(int u,int v)
    {
        jiedge++;
        edge[jiedge].v=v;
        edge[jiedge].next=head[u];
        head[u]=jiedge;
    }
    int dfs(int u,int pre)
    {
        vis[u]=1;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v!=pre)
            {
                if(vis[v])
                {
                    huan[num++]=v;
                    return v;
                }
                else
                {
                    huan[num++]=v;
                    int ans=dfs(v,u);
                    if(ans)
                    return ans;
                    num--;
                }
            }
        }
        return 0;
    }
    struct gg
    {
        int x,step;
    }a[5010],b,c;
    int main()
    {
        memset(vis,0,sizeof(vis));
        jiedge=0;
        memset(head,0,sizeof(head));
        int n,i,t;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        num=0;
        huan[num++]=1;
        int st=dfs(1,0);
        queue<gg>q;
        memset(vis,0,sizeof(vis));
        for(t=0;t<num;t++)
        if(huan[t]==st)
        break;
        for(i=t;i<num;i++)
        {
            a[i].x=huan[i],a[i].step=0;
            q.push(a[i]);
            vis[a[i].x]=1;
            vis[huan[i]]=1;
        }
        while(!q.empty())
        {
            b=q.front();
            q.pop();
            ans[b.x]=b.step;
            for(i=head[b.x];i;i=edge[i].next)
            {
                int v=edge[i].v;
                if(!vis[v])
                {
                    vis[v]=1;
                    c.x=v;
                    c.step=b.step+1;
                    q.push(c);
                }
            }
        }
        for(i=1;i<=n;i++)
        printf("%d%c",ans[i],i==n?'
    ':' ');
        return 0;
    }
    View Code
  • 相关阅读:
    lsblk---列出所有可用块设备的信息,
    blkid---对系统块设备信息查询
    du---是对文件和目录磁盘使用的空间查看
    strings---对象文件或二进制文件中查找可打印的字符串
    which---查找并显示给定命令的绝对路径
    whereis---定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。
    cd---切换工作目录
    cp---复制文件
    ls---显示文件目录各项信息
    pwd---以绝对路径的方式显示用户当前工作目录
  • 原文地址:https://www.cnblogs.com/jhz033/p/5444312.html
Copyright © 2020-2023  润新知