-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path割点与桥.cpp
158 lines (134 loc) · 3.15 KB
/
割点与桥.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::sort;
using std::min;
using std::max;
using std::swap;
// 实验采用DFS框架完成
enum COLOR { white, gray, black };
struct Node
{
int discover; // 被发现时间
int back; // 不通过父节点能够访问到的祖先的最小时间戳
int parent; // 父节点
COLOR color;
};
struct Edge
{
int v;
int w;
};
void show_data(const vector<vector<int> >& data);
int DFS(int x, vector<Node>& node, const vector<vector<int> >& data);
void show_node(const vector<Node>& node);
int t(0);
vector<int> articulation_point;
vector<Edge> bridge;
int main()
{
vector<Node> node;
vector<vector<int> > data;
int n(0), x(0), y(0);
cin >> n;
for (int i(0); i < n; ++i)
{
data.push_back(*(new vector<int>));
Node x{ 0,0,0, white };
node.push_back(x);
}
while (cin >> x >> y)
{
data[x].push_back(y);
data[y].push_back(x);
}
// show_data(data);
// 正确性已经得到了验证
DFS(0, node, data);
int root_time(0);
for (int i(0); i < node.size(); ++i)
{
if (node[i].parent == 0)
++root_time;
}
if (root_time == 1)
articulation_point.push_back(0);
sort(articulation_point.begin(), articulation_point.end());
for (int i(0); i < bridge.size(); ++i)
{
for (int j(0); j < bridge.size() - 1; ++j)
{
if (bridge[j].v > bridge[j + 1].v)
swap(bridge[j], bridge[j + 1]);
}
}
cout << "wo yi yue du guan yu chao xi de shuo ming" << endl;
for (int i(0); i < articulation_point.size(); ++i)
cout << articulation_point[i] << endl;
for (int i(0); i < bridge.size(); ++i)
{
cout << bridge[i].v << " " << bridge[i].w << endl;
}
return 0;
}
void show_data(const vector<vector<int>> & data)
{
for (int i(0); i < data.size(); ++i)
{
cout << i << " : ";
for (int j(0); j < data[i].size(); ++j)
cout << data[i][j] << " ";
cout << endl;
}
return;
}
int DFS(int x, vector<Node> & node, const vector<vector<int>> & data)
{
node[x].color = gray;
++t;
node[x].discover = t;
node[x].back = t;
// show_node(node);
for (int i(0); i < data[x].size(); ++i)
{
if (node[data[x][i]].color == white)
{
node[data[x][i]].parent = x;
// cout << x << "->" << data[x][i] << endl;
node[data[x][i]].back = DFS(data[x][i], node, data);
if (node[data[x][i]].back >= node[x].discover && x != 0)
{
// cout << "output" << " " << x << endl;
// show_node(node);
articulation_point.push_back(x);
}
node[x].back = min(node[x].back, node[data[x][i]].back);
if (node[x].discover < node[data[x][i]].back)
{
Edge e{ min(x, data[x][i]), max(x, data[x][i]) };
bridge.push_back(e);
}
}
else
{
if (node[x].parent != data[x][i])
{
// cout << x << " back to " << data[x][i] << endl;
node[x].back = min(node[data[x][i]].discover, node[x].back);
}
}
}
return node[x].back;
}
void show_node(const vector<Node>& node)
{
for (int i(0); i < node.size(); ++i)
{
cout << i << " discover:" << node[i].discover << " back:" << node[i].back << endl;
}
return;
}