標籤:
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+10, M = 1e3+10, mod = 1000000, inf = 1e9+1000;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]=1; for(int i=0;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]]=0; hav[scc]++; belong[q[top]]=scc; }while(x!=q[top--]); }}void Tarjan() { dfs(1,-1);}int dist[N],vis[N];void spfa(int u) { queue<int >q; q.push(u); for(int i=0;i<=n;i++) { dist[i] = inf, vis[i] = 0; } dist[0] = 0; vis[0] = 1; while(!q.empty()) { int k = q.front(); q.pop(); vis[k] = 0; for(int j=0;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] = 1; q.push(to); } } } }}void solve() { for(int i=1;i<=n;i++) {//cout<<belong[i]<<endl; for(int j=0;j<G[i].size();j++) { int a = i; int b = G[i][j]; if(hav[belong[a]]<=1&&hav[belong[b]]<=1) { E[a].push_back(make_pair(b,1)); } else if(hav[belong[a]]<=1) { E[a].push_back(make_pair(0,1)); } else if(hav[belong[b]]<=1) { E[0].push_back(make_pair(b,1)); } } } spfa(0); for(int i=1;i<=n;i++) { if(dist[i]==inf) cout<<0<<" "; else cout<<dist[i]<<" "; }}int main() { scanf("%d",&n); for(int i=1;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 0;}
Codeforces Beta Round #95 (Div. 2) D. Subway 邊雙聯通+spfa