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. C - Between the Offices

    Problem description As you may know, MemSQL has American offices in both San Francisco and Seattle. ...

  2. 苹果html上传后图片旋转问题

    最近做移动web项目但是遇到在苹果设备上html上传图片后,图片传到后台是旋转的 旋转角度不一,因此再次 读取照片时,无法正常显示,目前已经找到解决方法,至于原因看不太懂 翻译过来也是完全按照单词翻译 ...

  3. 《Linux程序设计》笔记(二)shell程序设计

    1. 进程树形显示 ps -e f 2. 重定向 > 覆盖文件 >> 附加至文件 1> 标准输出 2> 标准错误输出 0 代表一个程序的标准输入 3. 程序可以在当前目录 ...

  4. 创建100个目录dir1-dir100一键完成

    创建100个目录dir1-dir100将系统中已有文件xxx.txt复制1000份1.txt-1000.txt将文件1-10保存到第一个目录中11-20保存到第三个目录中的形式将所有文件处理完 #!/ ...

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

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

  6. 通过分析反汇编还原 C 语言 if…else 结构

    让我们从反汇编的角度去分析并还原 C 语言的 if - else 结构,首先我们不看源代码,我们用 OllyDBG 载入 PE 文件,定位到 main 函数领空,如下图所示. 在图示中,我已经做好了关 ...

  7. Linux C(day01)

    Linux是一个和Windows类似的操作系统 通常通过终端软件使用Linux操作系统 终端软件里只能使用键盘不能使用鼠标 可以在终端软件里输入各种命令控制计算机 完成各种任务 clear命令可以清除 ...

  8. Ecshop模板中html_options用法详解

    程序部分 <?php $smarty->assign('status_list', $_LANG['cs']); // 订单状态 $smarty->display("ind ...

  9. Beautifulsoup提取特定丁香园帖子回复

    DataWhale-Task3(Beautifulsoup爬取丁香园) 简要分析 完整代码 结果图 参考资料 简要分析 任务3:爬取丁香园论坛特定帖子,包括帖子主题,帖子介绍,回贴内容(用户名,用户头 ...

  10. TCriticalSection(Delphi)

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 临界区对象TCriticalSection(Delphi) 与 TRtlCritical ...