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. easyUI的datebox添加清空按钮功能

    需要修改源码: 第一步:按下图修改 第二步:按下两图修改(*zh_CN.js)

  2. 字符串最长子串匹配-dp矩阵[转载]

    转自:https://blog.csdn.net/zls986992484/article/details/69863710 题目描述:求最长公共子串,sea和eat.它们的最长公共子串为ea,长度为 ...

  3. [LeetCode] 605. Can Place Flowers_Easy

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  4. collections模块(收藏)

    collections是Python内建的一个集合模块,提供了许多有用的集合类. 1. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >> ...

  5. 消息 8101,级别 16,状态 1,第 1 行 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'ResourceInfo'中的标识列指定显式值。

    问题分析: 意思是你的主键是自动编号类型的,所以不能向该列插入数据. 解决办法: 执行 语句 :SET IDENTITY_INSERT CUSTOMER_TBL ON 然后在向表中插入数据,如inse ...

  6. NPOI 导出excel 分表

    /// <summary> /// 由DataTable导出Excel[超出65536自动分表] /// </summary> /// <param name=" ...

  7. <Convolutional Neural Network for Paraphrase Identification>

    code:https://github.com/chantera/bicnn-mi Yin的这篇论文提出了一种叫Bi-CNN-MI的架构,其中Bi-CNN表示两个使用Siamese框架的CNN模型:M ...

  8. redis windows版本下载

    https://github.com/dmajkic/redis/downloads http://windows.php.net/downloads/pecl/snaps/redis/3.1.4rc ...

  9. 关于安装VS2010过程中的错误

    下午本来安装好了VS:但是后来由于自己更新太多功能:直接使得VS太卡打不开:卸载重装:但是卸载的时候在“开始”里面的帮助文档和一些目录在卸载项里面没有:而在“开始"菜单就有:所以我索性把整个 ...

  10. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...