Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs
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.
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.
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.
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
题意:给你一个无向图,只有一个环,求各个点到环的最短距离;
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的更多相关文章
- Codeforces Beta Round #95 (Div. 2) D.Subway
题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...
- 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 ...
- codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)
题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #i ...
- 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 " ...
- 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 ...
- 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 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
随机推荐
- Approximate Inference
1. Approximation Probabilistic model 中的一个 central task :给定一组observation X 后,计算latent variables ...
- opencv3寻找最小包围矩形在图像中的应用-滚动条
#include<opencv2/opencv.hpp> #include<iostream> #include<vector> using namespace c ...
- ftp.GetResponse() 无法连接到远程服务器
最近在做一个ftp上传下载以及在服务器上创建文件夹的工具 报 GetResponse() 无法连接到远程服务器 错误 明明 ip , 账户和 密码 用ftp 工具都能连接上 ,可是 代码就不行了,看 ...
- 概率检索模型:BIM+BM25+BM25F
1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...
- 20155334 2016-2017-2 《Java程序设计》第八周学习总结
20155334 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章:NIO与NIO2 NIO的定义: InputStream.OutputStream ...
- tomcat警告WARNING: An attempt was made to authenticate the locked user "user"
后台出现很多警告WARNING: An attempt was made to authenticate the locked user "user"Jul 19, 2017 2: ...
- JavaScript的 基本数据类型---对象
第一:Javascript对象是 第二:Javascript中 第三:Javascript的对象是数据: 第四:JavaScript 中的对象可以简单理解成"名称:值"对(name ...
- keepalived+lvs高可用集群
LVS+Keepalived 介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国 ...
- 关于webpack的path和publicPath。
path:所有输出文件的目标路径; publicPath:输出解析文件的目录,url 相对于 HTML 页面 区别:path是webpack所有文件的输出的路径,必须是绝对路径,比如:output输出 ...
- python序列化数据
在python中序列化数据可以使用两种不同模块,一种是json格式,另一种是pickle模块! 序列化的概念: 序列化:将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON ...