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++. ...
随机推荐
- spring的统一异常处理
在控制器(controller)中添加统一异常处理的方法,在方法前面加@ExceptionHandler[(异常类.class)]注解
- [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- Java-二分查找算法
package com.lym.binarySearch; import java.util.Arrays; /** * 二分查找 * * @author Administrator * */ pub ...
- js五星好评
一般我们在一些购物以及美食的网站都会看到五星好评之类的,一下是使用js制作的五星好评! <!DOCTYPE html> <html lang="en"> & ...
- js数组之从数组中删除元素
使用pop()这个函数可以从数组中删除末尾的元素,shift方法可以删除数组中第一个元素.这些都是js中自带的函数,如果不使用这些函数的话,自己写的代码效率会很低的. <html> < ...
- BP神经网络的Java实现(转载)
神经网络的计算过程 神经网络结构如下图所示,最左边的是输入层,最右边的是输出层,中间是多个隐含层,隐含层和输出层的每个神经节点,都是由上一层节点乘以其权重累加得到,标上“+1”的圆圈为截距项b,对输入 ...
- 常用linux命令:locate 命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- C/C++之内存对齐
数据对齐,是指数据所在的内存地址必须是该数据长度的整数倍.DWORD数据的内存起始地址能被4除尽,WORD数据的内存起始地址能被2除尽.X86 CPU能直接访问对齐的数据,当它试图访问一个未对齐的数据 ...
- ES6学习--函数剩余参数 (rest参数)
ES6 引入 rest 参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了.rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中.(可以拿到除 ...
- Jquery 数组与字符串之间的转换
var auth_list = []; $("input[name='auth_list']:checkbox").each(function () { if ($(this).a ...