Subway

CodeForces - 131D

A subway scheme, classic for all Berland cities is represented by a set of nstations 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 

sol:这个数据范围实在是太友好了,O(n)bfs找环,n2dfs水过无压力,不知道(我猜)有更好的做法(树剖)可以nlogn
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=,inf=0x3f3f3f3f;
int n;
namespace Pic
{
int tot=,Next[M],to[M],head[N],Deg[N]; inline void add(int x,int y);
inline void dfs(int x,int fa);
inline void Solve(); inline void add(int x,int y)
{
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
Deg[y]++;
}
bool Vis[N];
inline void bfs()
{
int i;
queue<int>Queue;
for(i=;i<=n;i++) if(Deg[i]==)
{
Queue.push(i);
Vis[i]=;
}
while(!Queue.empty())
{
int x=Queue.front(); Queue.pop();
for(i=head[x];i;i=Next[i]) if(!Vis[to[i]])
{
Deg[to[i]]--;
if(Deg[to[i]]<)
{
Vis[to[i]]=;
Queue.push(to[i]);
}
}
}
}
int Ans[N];
bool Huan[N];
inline int dfs(int x,int fa,int Dis)
{
if(Huan[x]) return Dis;
int i,res=inf;
for(i=head[x];i;i=Next[i]) if(to[i]!=fa)
{
res=min(res,dfs(to[i],x,Dis+));
}
return res;
}
inline void Solve()
{
int i;
bfs();
for(i=;i<=n;i++) if(!Vis[i]) Huan[i]=;
for(i=;i<=n;i++) Ans[i]=dfs(i,,);
for(i=;i<=n;i++) W(Ans[i]);
}
}
int main()
{
int i;
R(n);
for(i=;i<=n;i++)
{
int x,y; R(x); R(y);
Pic::add(x,y); Pic::add(y,x);
}
Pic::Solve();
return ;
}
/*
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
*/
 

codeforces131D的更多相关文章

随机推荐

  1. SQL优化 MySQL版 - 避免索引失效原则(一)

    避免索引失效原则(一) 精力有限,剩余的失效原则将会在 <避免索引失效原则(二)>中连载出来,请谅解 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 避免索引失效的一些原 ...

  2. SmartCode 使用常见问题

    SmartCode 常见问题 SmartCode 能干什么? SmartCode = IDataSource -> IBuildTask -> IOutput => Build Ev ...

  3. C#语法——委托,架构的血液

    本篇文章主要介绍委托的应用. 委托是大家最常见的语法了,但会用与精通之间的差别是巨大的. 一个程序员如果不能精通委托,那么,他永远无法成为高级程序员. 所以,让我们把委托刻到血液里吧. 这样,你才能称 ...

  4. 如何用cmake编译

    本文由云+社区发表 作者:工程师小熊 CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake ...

  5. java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介

    本文对常用的反编译工具进行简单介绍 JD-GUI.procyon-decompiler.luyten.crf   反编译工具分类 JD-GUI JDK7以及之前可以使用   JD-GUI,如果版本&g ...

  6. 38.QT-QAxObject快速写入EXCEL示例

    参考链接:https://blog.csdn.net/czyt1988/article/details/52121360 http://blog.sina.com.cn/s/blog_a6fb6cc9 ...

  7. java爬虫系列第三讲-获取页面中绝对路径的各种方法

    在使用webmgiac的过程中,很多时候我们需要抓取连接的绝对路径,总结了几种方法,示例代码放在最后. 以和讯网的一个页面为例: xpath方式获取 log.info("{}", ...

  8. 类和对象,以及 LeetCode 每日一题

    所有类都是引用类型. 1 定义类 类是某一批对象的抽象. 1.1 定义类的语法: [修饰符] class 类名{ 零到多个构造器定义 零到多个成员变量 零到多个方法 } 对于一个类定义而言,可以包含三 ...

  9. MongoDB学习(使用分组、聚合和映射-归并)

    使用分组.聚合和映射-归并 MongoDB的强大功能之一,是直接在服务器对文档的值进行复杂的操作,而不用先发文档发送到客户端在进行处理. 结果分组 对大型数据集进行查询操作时,通常会根据文档的字段值对 ...

  10. 自动化批量管理工具pssh - 运维小结

    pssh提供OpenSSH和相关工具的并行版本.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括psshlib,可以在自定义应用程序中使用.pssh是python写的可以并发 ...