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 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.
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
6 1 2 3 4 6 4 2 3 1 3 3 5
0 0 0 1 1 2 大体题意: 给你一个图,这个图中只有一个环,求出每个点到环的最短距离 在做这个题时还有10分钟,没时间了,只能说对dfs的写法还是很不熟练! 思路: 借鉴了学长的博客 感觉真的写的好巧妙~~ 慢慢来吧! 首先是用vector建图。 开一个dist数组表示每个点表示走过的步数。 每一次dfs就给dist + 1. dfs中记录父节点和当前结点,然后遍历当前结点的儿子,发现没有符合要求 儿子和父节点不重复时,这时候已经是一个环了,return 0; 这样如果return 0,最后只返回到第一个节点(也就是目标,他的dist是1),所以输出dist -1 即可! 因为肯定存在一个环,所以最终一定能找回去,而找回去的找个点也一定是离目标最近的点! 输出dist-1即可! 可能描述也不太清楚,感觉dfs 还是自己看懂才好~ 详细见代码:#include<bits/stdc++.h> using namespace std; const int maxn = 3000 + 10; int d[maxn]; vector<int>g[maxn]; int dfs(int k,int fa,int dep){ d[k] = dep; int len = g[k].size(); for (int i = 0; i < len; ++i){ int u = g[k][i]; if (u == fa)continue; if (d[u])return d[u]; int kk = dfs(u,k,dep+1); if (kk)return kk; } return 0; } int main(){ int n; scanf("%d",&n); for (int i = 0 ;i < n; ++i){ int u,v; scanf("%d %d",&u,&v); g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i){ memset(d,0,sizeof d); if (i > 1)printf(" "); printf("%d",dfs(i,-1,1)-1); } return 0; }