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 ...
随机推荐
- 2015 多校赛 第五场 1006 (hdu 5348)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...
- 阿里云 CentOS 6.5 使用XAMPP 搭建LAMP环境
LAMP环境是常见的服务器环境,也是PHP网站常用的服务器环境,很多人喜欢手动配置,但是手动配置LAMP复杂.麻烦,简单一点的话可以使用集成环境.试了下LNMP的集成环境,用不习惯,另外由于本地一直使 ...
- 数据连接类 这里采用mysql
数据库通用操作类,自己参照几个通用类改写的,用起来还是蛮不错.... 这里用的mysql 要是其他数据库自行更改下就好 public class MySqlHelper { public stati ...
- C# Datetime 使用详解
获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...
- 使用 C# 进行 HTTP 操作
说明 主要使用到了 Newtonsoft.Json 和 System.Net 两个命名空间. Program.cs static void Main(string[] args) { WebOpert ...
- (2)dotnet开源电商系统-brnshop VS nopCommerce(dotnet两套电商来PK--第二篇:代码从哪开始-BrnMall3.0Beta)
看大牛们的源码,对于水平一般的人,还是略微有点难度的.我从我自身读码的亲身体验,写下杂散片语,希望能和大家一同进步,也为了日后记忆上的备查. 先看的是brnMall的源码结构,从哪看起呢? 首先推荐看 ...
- Polymorphism (computer science)
In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much&qu ...
- 修改默认的gitlab clone地址,要不每次都得自己修改
这个是无法clone的,得换成gitlab的ip地址 下面进行修改 sudo vim /opt/gitlab/embedded/service/gitlab-rails/config/ ...
- java操作Excel的poi 格式设置
格式设置 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi. ...
- Apex语言(三)原始数据类型
1.原始数据类型(Primitive) 整数:Integer 双精度:Double 单精度:Decimal 长整型:Long 日期:Date 日期时间:Datetime 字符串:String ID:I ...