Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa
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 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.
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
#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的更多相关文章
- Codeforces Beta Round #95 (Div. 2) D.Subway
		
题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...
 - 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 ...
 - 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 #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++. ...
 - Codeforces Beta Round #77 (Div. 2 Only)
		
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
 
随机推荐
- html body中的标签
			
HTML中的标签有两类 一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup> ...
 - MVC微信浏览器图片上传(img转Base64)
			
因公司业务需要,需要做一个微信公众号里的图片上传功能,主要用到的技术就是 img转base64 上到服务器 话不多说, 贴代码 先看前端显示出来的东西 OK 图片不重要,看代码 <!--微信图片 ...
 - Oracle 数据导入导出(imp/exp)
			
环境:windows下,oracle11g 1.启动oracle服务 net start OracleDBConsoleorclnet start OracleOraDb11g_home1TNSLis ...
 - 初学Hibernate杂乱总结
			
1.如果在"one"方中(如部门)写有Set属性,但是没有在映射文件中配置,那么,在获取指定部门下的所有员工时,不会报错,但是,Set内的元素个数为0.输出为"[]&qu ...
 - android黑科技系列——微信定位聊天记录中照片的位置信息插件开发详解
			
一.前言 最近关于微信中,朋友之间发送原图就可能暴露你的位置信息,其实这个问题不在于微信,微信是为了更好的体验效果,才有发送原图功能,而对于拍照,发送普通图片微信后台都会过滤图片的exif信息,这样就 ...
 - Excel 批量出来数据
			
try { string sheetname = TextBox1.Text.Trim(); HttpPostedFile upLoadPostFile = FileUpload1.PostedFil ...
 - Dijkstra的双栈算术表达式求值算法 C++实现
			
#include<iostream> #include<string> using namespace std; template<typename T> clas ...
 - Unity与Android通信的中间件
			
2.1.1 Fragment和Activity都需要实现的接口——IBaseView/** * Description:Basic interface of all {@link Activity} ...
 - Android 性能测试初探(二)
			
书接前文 Android 性能测试初探(一).上回大体介绍了下在 android 端的性能测试项,现在我们就细节测试项做一些阐述(包括如何自己 DIY 测试). 首先我们来说说启动时间.关于应用的启动 ...
 - PAT_A1137#Final Grading
			
Source: PAT A1137 Final Grading (25 分) Description: For a student taking the online course "Dat ...