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的更多相关文章
随机推荐
- 【面试】吃透了这些Redis知识点,面试官一定觉得你很NB(干货 | 建议珍藏)
万字长文,干货满满. 是数据结构而非类型 很多文章都会说,redis支持5种常用的数据类型,这其实是存在很大的歧义.redis里存的都是二进制数据,其实就是字节数组(byte[]),这些字节数据是没有 ...
- Python3 日期与时间戳相互转换
开发中经常会对时间格式处理,对于时间数据,比如2019-02-28 10:23:29,有时需要日期与时间戳进行相互转换,在Python3中主要用到time模块,相关的函数如下: 其中unix_time ...
- Windows有点腻了?不如试试Ubuntu.
最近在接触Python. 因为担心环境会向Java一样,很容易影响当前的工作电脑. 所以准备搭建一台虚拟机,不过Windows的尺寸是在太大了.所以,选择安装Ubuntu. Ubuntu官方网站地址: ...
- android 记一次解决键盘遮挡问题
文章链接:https://mp.weixin.qq.com/s/1gkMtLu0BTXOUOj6isDjUw 日常android开发过程中,会遇到编辑框输入内容弹出软键盘,往往会出现键盘遮挡内容,或者 ...
- DVWA 黑客攻防演练(十三)JS 攻击 JavaScript Attacks
新版本的 DVWA 有新东西,其中一个就是这个 JavaScript 模块了. 玩法也挺特别的,如果你能提交 success 这个词,成功是算你赢了.也看得我有点懵逼. 初级 如果你改成 " ...
- Sql Server 完全卸载,卸载干净,Windows 7。
一般卸载程序的卸载方法无法完全清除SqlServer 导致重装sqlserver报错,下面给大家介绍和讲解如何完全卸载Sql Server. (此教程使用的系统为Windows 7[Win10,Win ...
- 浏览器仿EXCEL表格插件 - 智表ZCELL产品V1.4发布
智表(zcell)是一款浏览器仿excel表格jquery插件.智表可以为你提供excel般的智能体验,支持双击编辑.设置公式.设置显示小数精度.下拉框.自定义单元格.复制粘贴.不连续选定.合并单元格 ...
- 总结JAVA----IO流中的字节流
对于IO流中字节流的总结 字节流的概念 由于应用程序,经常需要和文件打交道,所以Inputstream专门提供了读写文件的子类:FileInputStream和FileOutputStream类,如果 ...
- Android 获取验证码倒计时实现
Android 获取验证码倒计时实现 2017年10月24日 09:55:41 FBY展菲 阅读数:2002 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- 教程一 openwrt路由器入门 远程命令行+文件系统
如图,拿到一个openwrt路由器我们第一步要远程控制. 这里在买了两块wifi-robots wifi视频模块. 0首先说下这个WIIF的信息 淘宝购买链接 https://item.taobao ...