zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    在vs2010里使用EF4.3的Code First个人使用笔记
    如何充分利用C#匿名方法的平台优势(转)
    打开Microsoft SQL Server Management Studio 2005非常慢,特别慢的原因
    PowerShell msbuild
    很久没写了
    第一篇博客,逗大家一笑
    以编程方式调用按钮1(button1)的 Click 事件
    VS2010删除已安装的联机模板
    创建和分享你的Visual Studio color
    异步获取CMD命令行输出内容
  • 原文地址:https://www.cnblogs.com/jhz033/p/5444312.html
Copyright © 2011-2022 走看看