D. Subway
 

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.

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 thatxi ≠ 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
input
4
1 3
4 3
4 2
1 2
output
0 0 0 0 
 
题意
  
  给你一个n点n边的无向图,里边必定有一个环,让你求每个点到这个环上的距离
 
题解:
 
  我们跑一发tarjan求出环是哪些点,再重新建图求最短路
  或者你可以dfs求出环,再dfs求距离或者bfs求距离
 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include<queue>
using namespace std;
const int N = 1e5+, M = 1e3+, mod = , inf = 1e9+;
typedef long long ll;
int n,dfn[N],low[N],cnt,scc,iscut[N],d[N],q[N],top,belong[N],hav[N],inq[N];
vector<int > G[N];
vector<pair<int,int> > E[N];
void dfs(int x,int fa) {
dfn[x] = low[x] = ++cnt;
q[++top] = x;
inq[x]=;
for(int i=;i<G[x].size();i++) {
int to = G[x][i];
if(fa==to) continue;
if(!dfn[to]) {
dfs(to,x);
low[x] = min(low[x],low[to]);
}
else if(inq[to])low[x] = min(low[x],dfn[to]);
}
if(low[x]==dfn[x]) {
scc++;
do {
inq[q[top]]=;
hav[scc]++;
belong[q[top]]=scc;
}while(x!=q[top--]);
}
}
void Tarjan() {
dfs(,-);
}
int dist[N],vis[N];
void spfa(int u) {
queue<int >q;
q.push(u);
for(int i=;i<=n;i++) {
dist[i] = inf, vis[i] = ;
}
dist[] = ;
vis[] = ;
while(!q.empty()) {
int k = q.front();
q.pop();
vis[k] = ;
for(int j=;j<E[k].size();j++) {
int to = E[k][j].first;
int value = E[k][j].second;
if(dist[to]>dist[k]+value) {
dist[to] = dist[k]+value;
if(!vis[to]) {
vis[to] = ;
q.push(to);
}
}
}
}
}
void solve() {
for(int i=;i<=n;i++) {//cout<<belong[i]<<endl;
for(int j=;j<G[i].size();j++) {
int a = i;
int b = G[i][j];
if(hav[belong[a]]<=&&hav[belong[b]]<=) {
E[a].push_back(make_pair(b,));
}
else if(hav[belong[a]]<=) {
E[a].push_back(make_pair(,));
}
else if(hav[belong[b]]<=) {
E[].push_back(make_pair(b,));
} }
}
spfa();
for(int i=;i<=n;i++) {
if(dist[i]==inf) cout<<<<" ";
else cout<<dist[i]<<" ";
}
}
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) {
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
d[a]++;
d[b]++;
}
Tarjan();
solve();
return ;
}

Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa的更多相关文章

  1. Codeforces Beta Round #95 (Div. 2) D.Subway

    题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...

  2. Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs

    D. Subway A subway scheme, classic for all Berland cities is represented by a set of n stations conn ...

  3. codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)

    题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #i ...

  4. 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 " ...

  5. 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 ...

  6. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  7. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. springboot 的一般配置

    import javax.servlet.Filter; import org.springframework.boot.SpringApplication; import org.springfra ...

  2. CDC之fast->slow (1)

    Sampling slower signals into faster clock domains causes fewer potential problems than sampling fast ...

  3. gitlab变更邮箱后发送邮件报SSLError错误

    测试发送邮件: gitlab-rails console Notify.test_email('test666@example.com', 'Message Subject', 'Message Bo ...

  4. lucene多条件查询”搜索—BooleanQuery

    /** * “多条件查询”搜索—BooleanQuery * BooleanQuery也是实际开发过程中经常使用的一种Query. * 它其实是一个组合的Query,在使用时可以把各种Query对象添 ...

  5. 金蝶WAFII

  6. python3使用465端口发送邮件来解决阿里云封闭25端口问题

    import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr #发件人邮箱账号my_send ...

  7. 进程(day09)

    进程的管理 一.进程的基础 进程和程序的区别 每个进程有自己的pid.PCB 操作系统上运行的所有进程构成一颗树. 如何查看这颗树? pstree() 树根进程是init pid是 进程间的亲缘关系两 ...

  8. 09.正则表达式re-3.常用的匹配规则

    模式 描述 \w 匹配字母.数字及下划线 \W 匹配不是字母.数字及下划线的字符 \s 匹配任意空白字符,等价于[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意数字,等价于[0-9] \D ...

  9. 7.ES几种常见的搜索方式

    主要知识点  1, query string search (1)  GET /ecommerce/product/_search (2) GET/ecommerce/product/_search? ...

  10. Vue CLI 3 中文文档

    翻译文档 文档翻译全貌 前言 之前写了一篇Vue CLI 3.x 版本的简单体验,当时文档还不全,具体的使用方法并不是很清楚,大概是2月7号,收到Vue CLI 3接近Beta版的提示,作者尤雨溪也讲 ...