codeforces131D
Subway
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
4
1 3
4 3
4 2
1 2
0 0 0 0
6
1 2
3 4
6 4
2 3
1 3
3 5
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的更多相关文章
随机推荐
- 推荐三个 VSCode 摸鱼插件
周三是一周中最难以度过的一天,离上个周末过去了两天,离下个周末也还有两天.为了让各位更好地搬(mo)砖(yu),今天给大家推荐三款效(mo)率(yu)工(shen)具(qi)! 一.听歌插件 1 功能 ...
- 原生js轮盘抽奖实例分析(幸运大转盘抽奖)
效果图: 所需图片素材: 这张图是pointer.png的位置的. turntable-bg.jpg这张是转盘背景图,在背景位置. 这张是turntable.png位置的. 需要这三张图片,如果要实现 ...
- Jetty 开发指南:嵌入式开发示例
Jetty具有嵌入各种应用程序的丰富历史. 在本节中,我们将向您介绍我们的git存储库中的embedded-jetty-examples项目下的一些简单示例. 重要:生成此文档时,将直接从我们的git ...
- transform 的副作用
transform是一些效果的集合,主要是移动.旋转.缩放和倾斜这四种基本操作,还可以通过设置matrix矩阵来实现更复杂的效果. 变形transform可以实现2D和3D两种效果. 变形transf ...
- 利用AccessibilityService自动获取微信号(Android)
前言: 最近遇到一个需求,要求写一个小插件,能够自动在微信的页面弹出一个窗口,展示用户的相关信息(与我们公司有关的信息,方便运营快速了解用户信息). 当时我第一反应是不可能,如果能够在别的app中获取 ...
- git错误--ssh: Could not resolve hostname ssh.github.com: Name or service not known--解决方式
错误如下: git push origin ssh: Could not resolve hostname ssh.github.com: Name or service not known fata ...
- vue框架构建项目流程
构建项目流程: 1.全局查询:node -v 2.全局初始化:npm install --global vue-cli 3.模块化工程:vue init webpack myapp--->y,n ...
- GlusterFS群集存储项目
最小化安装的centos7.5 内存大于1GB 关闭selinux,防火墙端口放行(port:24007,111)(测试建议关闭firewalld) 一.环境部署 所有软件包离线安装,原因是yum安装 ...
- VS code 中的各种变量 ${file},${fileBasename}
VS code 中的各种变量 ${file},${fileBasename} 2017年08月24日 11:14:07 bailsong 阅读数:7108 from: https://blog. ...
- Redis与Memocache的区别
转载地址:http://gnucto.blog.51cto.com/3391516/998509 Redis与Memcached的区别 传统MySQL+ Memcached架构遇到的问题 实际MySQ ...