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 that xi ≠ 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
  1. 4
    1 3
    4 3
    4 2
    1 2
output
  1. 0 0 0 0
input
  1. 6
    1 2
    3 4
    6 4
    2 3
    1 3
    3 5
output
  1. 0 0 0 1 1 2
    题意:给你一个无向图,只有一个环,求各个点到环的最短距离;
       dfs求环,bfs求距离;
     
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<cstring>
  9. #include<vector>
  10. #include<list>
  11. #include<set>
  12. #include<map>
  13. using namespace std;
  14. #define ll long long
  15. #define inf 2000000001
  16. int scan()
  17. {
  18. int res = , ch ;
  19. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  20. {
  21. if( ch == EOF ) return << ;
  22. }
  23. res = ch - '' ;
  24. while( ( ch = getchar() ) >= '' && ch <= '' )
  25. res = res * + ( ch - '' ) ;
  26. return res ;
  27. }
  28. int huan[],jiedge,num;
  29. int vis[];
  30. struct is{int v,next;};
  31. is edge[];
  32. int head[];
  33. int ans[];
  34. void addedge(int u,int v)
  35. {
  36. jiedge++;
  37. edge[jiedge].v=v;
  38. edge[jiedge].next=head[u];
  39. head[u]=jiedge;
  40. }
  41. int dfs(int u,int pre)
  42. {
  43. vis[u]=;
  44. for(int i=head[u];i;i=edge[i].next)
  45. {
  46. int v=edge[i].v;
  47. if(v!=pre)
  48. {
  49. if(vis[v])
  50. {
  51. huan[num++]=v;
  52. return v;
  53. }
  54. else
  55. {
  56. huan[num++]=v;
  57. int ans=dfs(v,u);
  58. if(ans)
  59. return ans;
  60. num--;
  61. }
  62. }
  63. }
  64. return ;
  65. }
  66. struct gg
  67. {
  68. int x,step;
  69. }a[],b,c;
  70. int main()
  71. {
  72. memset(vis,,sizeof(vis));
  73. jiedge=;
  74. memset(head,,sizeof(head));
  75. int n,i,t;
  76. scanf("%d",&n);
  77. for(i=;i<n;i++)
  78. {
  79. int u,v;
  80. scanf("%d%d",&u,&v);
  81. addedge(u,v);
  82. addedge(v,u);
  83. }
  84. num=;
  85. huan[num++]=;
  86. int st=dfs(,);
  87. queue<gg>q;
  88. memset(vis,,sizeof(vis));
  89. for(t=;t<num;t++)
  90. if(huan[t]==st)
  91. break;
  92. for(i=t;i<num;i++)
  93. {
  94. a[i].x=huan[i],a[i].step=;
  95. q.push(a[i]);
  96. vis[a[i].x]=;
  97. vis[huan[i]]=;
  98. }
  99. while(!q.empty())
  100. {
  101. b=q.front();
  102. q.pop();
  103. ans[b.x]=b.step;
  104. for(i=head[b.x];i;i=edge[i].next)
  105. {
  106. int v=edge[i].v;
  107. if(!vis[v])
  108. {
  109. vis[v]=;
  110. c.x=v;
  111. c.step=b.step+;
  112. q.push(c);
  113. }
  114. }
  115. }
  116. for(i=;i<=n;i++)
  117. printf("%d%c",ans[i],i==n?'\n':' ');
  118. return ;
  119. }

Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs的更多相关文章

  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 边双联通+spfa

    D. Subway   A subway scheme, classic for all Berland cities is represented by a set of n stations co ...

  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 #94 div 2 C Statues dfs或者bfs

    C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. python 皮尔森相关系数

    皮尔森理解 皮尔森相关系数(Pearson correlation coefficient)也称皮尔森积矩相关系数(Pearson product-moment correlation coeffic ...

  2. 转载自(http://snailz.diandian.com/post/2012-10-24/40041265730)

    PHP 5.4.8 添加系统服务命令 之前没注意,PHP 5.4.8 的安装包有自带的系统服务注册文件的 打开编译安装包,换成你自己的路径 cd /mydata/soft/php-5.4.8/ cp ...

  3. 008-centos服务管理

  4. 文件操作(CRT、C++、WIN API、MFC)

    一.使用CRT函数文件操作 二.使用标准C++库 std::fstream std::string 1)std::string对象内部存储了一个C的字符串,以'\0'结尾的. 2)std::strin ...

  5. Oracle性能优化之HINT的用法

    1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...

  6. Python 基本数据类型(2)

    知识内容: 1.python对象模型 2.数字与bool 3.字符串 4.列表与元组 5.字典与集合 一.python对象模型 1.python对象模型 对象是python语言中最基本的概念,在pyt ...

  7. linux基础命令---bzip2

    bzip2 使用Burrows-Wheeler块排序文本压缩算法,将文件进行压缩,压缩比率比一般算法高一些.bzip2要求命令行标志附带一个文件名列表.每个文件都被自己的压缩版本替换,名称为“orig ...

  8. MongoDB ----基于分布式文件存储的数据库

    参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...

  9. nginx做http向https的自动跳转

    在访问百度时,在浏览器输入www.baidu.com会自动跳转到https://www.baidu.com不用人工干预,nginx也可以做这样的自动跳转! 首先让nginx服务器监听两个端口,分别是8 ...

  10. Ubuntu Linux系统环境变量配置文件

    Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...