Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa

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

41 34 34 21 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;
}
时间: 2024-10-23 21:15:53

Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa的相关文章

codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)

题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #include <cstring> char s[110]; bool islow(char c) { return c >= 'a' && c <= 'z'; } char up(char c) { return c - 32; } char low(char c) {

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1