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. Scala学习2 ———— 三种方式完成HelloWorld程序

    三种方式完成HelloWorld程序 分别采用在REPL,命令行(scala脚本)和Eclipse下运行hello world. 一.Scala REPL. 按照第一篇在windows下安装好scal ...

  2. MySQL实现递归查询

    DROP FUNCTION IF EXISTS queryChildrenCaseInfo;CREATE FUNCTION queryChildrenCaseInfo(cId INT)RETURNS ...

  3. bootstrap 图片 图标

    一.图片 1.响应式图片:<img src="  " class="responsive"> 2.圆角图片:<img src="  ...

  4. 最简单的多线程死锁案例代码(Java语言)

    package com.thread.test; public class DeadLock { private static Object firstMonitor = new Object(); ...

  5. C#调用FFMPEG实现桌面录制(视频+音频+生成本地文件)

    不得不说FFMPEG真是个神奇的玩意,所接触的部分不过万一.网上有个很火的例子是c++方面的,当然这个功能还是用c++来实现比较妥当. 然而我不会c++ 因为我的功能需求比较简单,只要实现基本的录制就 ...

  6. 使用Ajax验证用户名

    Ajax是一项很重要的技术,下面简要举个例子,来解释如何使用Ajax.步骤如下:使用Ajax验证用户名使用文本框的onBlur事件 使用Ajax技术实现异步交互创建XMLHttpRequest对象通过 ...

  7. C++多行文本读取

    使用的多行读取的代码如下: //读取文本浮点数到多个模式 序列 bool CPicToolsDlg::readTxt2SeqMulti( std::string TxtName, std::vecto ...

  8. Visual Studio Code 插件推荐

    Path Intellisense - 路径补全 HTML Snippets - HTML 标记增强 Markdown+Math - Markdown 增强(数学表达式) vscode-icons - ...

  9. 阿里巴巴矢量库IconFont__使用小录

    使用阿里巴巴矢量库方法虽然不难,但本人记性不好,遂在次记录几笔 阿里巴巴矢量库地址:http://www.iconfont.cn/plus 阿里巴巴矢量库图标好处: 1:图标矢量化 2:自己总结:ic ...

  10. Centos6.6 编译安装nginx

    一.基本环境 nginx 1.9版以后增加了一些新的特性,支持tcp负载均衡,不过这次还是用1.8.0,这里面有个memcached的代理模块,有时间再测试下 1.centos6.6 2.nginx1 ...