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的更多相关文章
随机推荐
- Cookie浅析
Cookie 翻阅了好久关于Cookie的博客及文档,感觉一直有一块结没有解开,所以一直难以在脑中形成一个顺畅的知识脉络.最后实在是遭不住,拉上我的大神朋友在食堂里坐了3个小时,问了个底朝天!总算形 ...
- Tomcat的常用内置对象
Tomcat的常用内置对象 1.request内置对象 所谓内置对象就是容器已经创建好了的对象,如果收到一个用户的请求就会自动创建一个对象来处理客户端发送的一些信息,这个内置对象就是request.类 ...
- .net开源工作流引擎ccflow Pop返回值设置
关键词: 点击字段弹出返回值填充文本框或其他字段 表单自动填充 .net开源工作流 jflow工作流 ccflow 工作流引擎 应用场景 当我们的查询信息比较多我们希望有一个比较 ...
- 斐波那契数列(C#)
斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:1.1.2.3.5.8.13 ...
- sqlsever存储过程配合代理作业自动定时建表
1.自动建表存储过程 USE [ThreeToOne] GO /****** Object: StoredProcedure [dbo].[WTO_CreateTable_ScanDoXXX] ...
- SQL SERVER-创建Alwayson
Failover Custer(AlwaysOn) 创建Failover Cluster和启动AlwaysOn 创建Failover Cluster 节点的个数要是奇数如果是偶数则要加一个仲裁磁盘.在 ...
- JDBC获取数据库连接
是什么? JDBC:Java Data Base Connectivity(java数据库连接) 为什么用? sun公司提供JDBC API接口,数据库厂商来提供实现 我们需要用哪个数据库就加载那个数 ...
- Windows Server(r12) - 配置 MySQL 远程访问
Windows Server(r12) - 配置 MySQL 远程访问 工作主要为两部分, 一部分是 Windows 防火墙, 一部分是 MySQL 自身 Windows 端口远程访问 其实就是在 W ...
- SQL server 2017使用教程
1.安装: 从https://www.microsoft.com/en-us/sql-server/sql-server-downloads官网下载sql server2017试用版 180天 安装完 ...
- WPF软件开发系统之二——水环境检测Surface触摸屏软件开发
该系统采用C#.WPF语言开发,开发工具Visual Studio 2015.Blend,环境WIN7系统及以上,适用于PC.Windows触摸屏Surface等设备. 部分截图效果如下: 开发工具环 ...