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 = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int huan[],jiedge,num;
int vis[];
struct is{int v,next;};
is edge[];
int head[];
int ans[];
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]=;
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 ;
}
struct gg
{
int x,step;
}a[],b,c;
int main()
{
memset(vis,,sizeof(vis));
jiedge=;
memset(head,,sizeof(head));
int n,i,t;
scanf("%d",&n);
for(i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
num=;
huan[num++]=;
int st=dfs(,);
queue<gg>q;
memset(vis,,sizeof(vis));
for(t=;t<num;t++)
if(huan[t]==st)
break;
for(i=t;i<num;i++)
{
a[i].x=huan[i],a[i].step=;
q.push(a[i]);
vis[a[i].x]=;
vis[huan[i]]=;
}
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]=;
c.x=v;
c.step=b.step+;
q.push(c);
}
}
}
for(i=;i<=n;i++)
printf("%d%c",ans[i],i==n?'\n':' ');
return ;
}

Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs的更多相关文章

  1. Codeforces Beta Round #95 (Div. 2) D.Subway

    题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...

  2. Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa

    D. Subway   A subway scheme, classic for all Berland cities is represented by a set of n stations co ...

  3. codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)

    题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #i ...

  4. Codeforces Beta Round #95 (Div. 2) C. The World is a Theatre 组合数学

    C. The World is a Theatre There are n boys and m girls attending a theatre club. To set a play " ...

  5. Codeforces Beta Round #95 (Div. 2) C 组合数学

    C. The World is a Theatre time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Beta Round #94 div 2 C Statues dfs或者bfs

    C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. Kafka核心组件

    一.Kafka核心组件及工作方式 Producer :消息生产者,就是向kafka broker发消息的客户端 Consumer :消息消费者,向kafka broker取消息的客户端 Topic : ...

  2. jmeter 测试websocket接口(一)

    jmeter 测试websocket接口时,需要对jmeter添加测试websocket的jar包. 下载地址: https://download.csdn.net/download/qq_14913 ...

  3. angular前端框架

    总所周知,在前端开发中,大家用的比较多的框架就是angular,vue,react等,今天就为大家讲一下angular大家框架的原理及运用 1.本次所举的例子是以依赖require.js的, < ...

  4. 转载 vsftpd安装

    http://blog.csdn.net/shutfuckingup/article/details/8250290 1:安装vsftpd    yum install vsftpd 2:关闭防火墙 ...

  5. linux make configure make

    开放源码:就是程序代码,写给人类看的程序语言,但机器并不认识,所以无法执行: 编译程序:将程序代码转译成为机器看得懂的语言,就类似编译者的角色: 可执行文件:经过编译程序变成二进制后机器看得懂所以可以 ...

  6. sql 关于存储过程的查询

    --查数据库中所有的存储过程select * from sys.procedures ----------------------查数据库中所有的存储过程select o.name from sysc ...

  7. DIV内容垂直居中

    css垂直居中属性设置vertical-align: middle对div不起作用,例如: <!DOCTYPE html> <html lang="zh-CN"& ...

  8. Linux服务器配置---安装centos

    安装centos 1.插入光盘,启动,可以选择第一项进行安装 2.根据实际需求,一般会选择skip 3.选择语言“简体中文” 4.选择第一项 5.设置主机名字,使用默认 6.选择时区 7.设置超级用户 ...

  9. 【CSS3】纯CSS代码实现模拟时钟,+js对时功能。

    使用CSS3纯代码来实现模拟时钟,及指针动画功能. 在这里主要使用到css3一些基本元素: border-radius:圆角边框,画圆形:表盘 Transform:变换,旋转,扭曲:刻度盘,指针形状 ...

  10. POI Excel文件的读取与写入

    1. 创建目录 if(!(new File(path).isDirectory())){ new File(path).mkdirs();} 2. 读取Excel文件,并进行写入操作 Workbook ...