-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgraph_center.cpp
43 lines (41 loc) · 1.16 KB
/
graph_center.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
const int INF = 1e9 + 9;
vector<vector<int>> adj;
struct GraphCenter {
int n, diam = 0;
vector<int> centros, dist, pai;
int bfs(int s) {
queue<int> q;
q.push(s);
dist.assign(n + 5, INF);
pai.assign(n + 5, -1);
dist[s] = 0;
int maxidist = 0, maxinode = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (dist[u] >= maxidist) maxidist = dist[u], maxinode = u;
for (int v : adj[u]) {
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
pai[v] = u;
q.push(v);
}
}
}
diam = max(diam, maxidist);
return maxinode;
}
GraphCenter(int st = 0) : n(adj.size()) {
int d1 = bfs(st);
int d2 = bfs(d1);
vector<int> path;
for (int u = d2; u != -1; u = pai[u]) path.push_back(u);
int len = path.size();
if (len % 2 == 1) {
centros.push_back(path[len / 2]);
} else {
centros.push_back(path[len / 2]);
centros.push_back(path[len / 2 - 1]);
}
}
};