基环树上求环

Posted 2020pengxiyue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基环树上求环相关的知识,希望对你有一定的参考价值。

题目描述

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.

题解

基环树:又称环套树,指的是由n个点和n条边组成的任意两点间都连通的图,比树多一条边,所以整个图中有且只有一个环。

在基环树上找环,我们可以通过一边DFS,在遍历过程中对于一条边所到达的点已经被访问过,且不是这条边的出发点则证明这条边在环上。而这条边的终点V上一次到这一次之间所经过的点连成路径便是一个环,而这个路径们可以通过栈来记录。

值得注意的是,一遍DFS只能求出一个环,而再求出环之后的DFS需要把这个环缩成一个点之后才能正常进行,这也是这个方法比较低效的方法。

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAX = 3005;
 5 int Head[MAX], Next[MAX << 1], To[MAX << 1], w[MAX << 1], edgenum = 0;
 6 void Add_edge(int from, int to)
 7     {
 8         Next[++ edgenum] = Head[from];
 9         Head[from] = edgenum;
10         To[edgenum] = to;
11     }
12 
13 int sta[MAX], top = 0, vis[MAX], huan[MAX], cnthuan = 0, flag;
14 void Get_huan(int u, int father)
15     {
16         if(flag) return;
17         sta[++ top] = u, vis[u] = 1;
18         for(int i = Head[u]; i != -1; i = Next[i])
19         {
20             int v = To[i];
21             if(v == father)    continue;
22             if(vis[v])    //有环
23             {
24                 huan[v] = ++cnthuan;
25                 for(;sta[top] != v; -- top)    huan[sta[top]] = cnthuan;
26                 top --;
27                 flag = 1;
28                 return;
29             }
30             Get_huan(v, u);
31             sta[-- top], vis[v] = 0;
32         }
33         return;
34     }
35 
36 int Deep[MAX];
37 void Get_Deep(int u)
38     {
39         for(int i = Head[u]; i != -1; i = Next[i])
40         {
41             int v = To[i];
42             if(Deep[v] || huan[v])    continue;
43             Deep[v] = Deep[u] + 1;
44             Get_Deep(v);
45         }
46     }
47 
48 int main()
49 {
50     //freopen("CF131D.in", "r", stdin);
51     //freopen("CF131D.out", "w", stdout);
52 
53     int n, x, y, colorhuan;
54     memset(Head, -1, sizeof(Head));
55     scanf("%d", &n);
56     for(int i = 1; i <= n; ++ i)
57     {
58         scanf("%d%d", &x, &y);
59         Add_edge(x, y);
60         Add_edge(y, x);
61     }
62     flag = 0;
63     Get_huan(1, 0);
64     for(int i = 1; i <= n; ++ i)
65     {
66         if(huan[i] != 0)
67         {
68             Deep[i] = 0;
69             Get_Deep(i);
70         }
71     }
72     for(int i = 1; i <= n; ++ i)    printf("%d ", Deep[i]);
73     printf("
");
74 }

 

以上是关于基环树上求环的主要内容,如果未能解决你的问题,请参考以下文章

H - Traffic Network in Numazu HDU - 6393(基环树)

题解 P1453 城市环路

基环树一统天下千秋万代

[题解] LuoguP4381 [IOI2008]Island

JoyOI1940 创世纪

图论小专题B