Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stack>
- using namespace std;
- const int maxn = 1e5 + 10;
- int n, m;
- vector<int> graph[maxn];
- int discoverable_time[maxn];
- int low_link[maxn];
- bool visited[maxn];
- int current_time = 0;
- void dfs(int node, int parent = -1) {
- visited[node] = true;
- discoverable_time[node] = current_time;
- low_link[node] = current_time;
- current_time++;
- int children = 0;
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neighbour = graph[node][i];
- if(neighbour != parent) {
- if(visited[neighbour]) {
- low_link[node] = min(low_link[node], discoverable_time[neighbour]);
- }
- else {
- dfs(neighbour, node);
- low_link[node] = min(low_link[node], low_link[neighbour]);
- if(parent != -1 and low_link[neighbour] >= discoverable_time[node]) {
- cout << "Articulation point: " << node + 1 << endl;
- }
- children++;
- }
- }
- }
- if(children > 1 and parent == -1) {
- cout << "Articulation point: " << node + 1 << endl;
- }
- }
- void articulation_points() {
- for(int i = 0; i < maxn; i++) {
- visited[i] = false;
- discoverable_time[i] = -1;
- low_link[i] = -1;
- }
- for(int i = 0; i < n; i++) {
- if(!visited[i]) {
- dfs(i);
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- articulation_points();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement