Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Graphs

Articulation Points

Hard · Graphs · Asked at Google, Amazon, Microsoft

An articulation point (cut vertex) of an undirected graph is a vertex whose removal increases the number of connected components. Print all articulation points in increasing order, or NONE if there are none. The graph may be disconnected.

Input

First line: n m. Next m lines: u v (no self-loops or repeated edges).

Output

The articulation points, space-separated, or NONE.

Constraints

Tarjan’s DFS with discovery times tin[] and low[] (the earliest time reachable using one back edge from the subtree). A non-root u is a cut vertex if some child v has low[v] ≥ tin[u]; the DFS root is one if it has two or more DFS children.

Example 1

Input

5 5
1 2
1 3
2 3
3 4
4 5

Output

3 4

Example 2

Input

4 4
1 2
2 3
3 4
4 1

Output

NONE

Hints

  1. Run a DFS and record when each vertex is first visited (tin).
  2. low[v] = the earliest tin reachable from v’s subtree using at most one back edge.
  3. If a child v cannot reach above u (low[v] ≥ tin[u]), removing u cuts v’s subtree off.

Approach

Tarjan's algorithm: DFS from every unvisited vertex. For each tree edge u → v, after visiting v, set low[u] = min(low[u], low[v]); for a back edge u → v, low[u] = min(low[u], tin[v]). A non-root u is a cut vertex if some child v has low[v] ≥ tin[u]. The root of each DFS tree is a cut vertex exactly when it has at least two DFS children. The Python version uses an explicit stack because a path-shaped graph is 10^4 levels deep.

Complexity: O(n + m) time · O(n + m) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int n, m, timer_ = 0;
vector<vector<int>> adj;
vector<int> tin, low;
vector<bool> cut;

void dfs(int u, int p) {
    tin[u] = low[u] = timer_++;
    int children = 0;
    for (int v : adj[u]) {
        if (v == p) continue;
        if (tin[v] >= 0) low[u] = min(low[u], tin[v]);
        else {
            dfs(v, u);
            low[u] = min(low[u], low[v]);
            if (p != 0 && low[v] >= tin[u]) cut[u] = true;
            children++;
        }
    }
    if (p == 0 && children >= 2) cut[u] = true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    adj.assign(n + 1, {});
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    tin.assign(n + 1, -1);
    low.assign(n + 1, 0);
    cut.assign(n + 1, false);
    for (int u = 1; u <= n; u++)
        if (tin[u] < 0) dfs(u, 0);
    string out;
    for (int u = 1; u <= n; u++)
        if (cut[u]) out += (out.empty() ? "" : " ") + to_string(u);
    cout << (out.empty() ? "NONE" : out) << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, m = data[0], data[1]
adj = [[] for _ in range(n + 1)]
for i in range(m):
    u, v = data[2 + 2 * i], data[3 + 2 * i]
    adj[u].append(v)
    adj[v].append(u)
tin = [-1] * (n + 1)
low = [0] * (n + 1)
cut = [False] * (n + 1)
timer = 0
for root in range(1, n + 1):
    if tin[root] >= 0:
        continue
    tin[root] = low[root] = timer
    timer += 1
    root_children = 0
    stack = [(root, 0, 0)]  # node, parent, next edge index
    while stack:
        u, p, k = stack[-1]
        if k < len(adj[u]):
            stack[-1] = (u, p, k + 1)
            v = adj[u][k]
            if v == p:
                continue
            if tin[v] >= 0:
                low[u] = min(low[u], tin[v])
            else:
                tin[v] = low[v] = timer
                timer += 1
                if u == root:
                    root_children += 1
                stack.append((v, u, 0))
        else:
            stack.pop()
            if stack:
                pu = stack[-1][0]
                low[pu] = min(low[pu], low[u])
                if pu != root and low[u] >= tin[pu]:
                    cut[pu] = True
    if root_children >= 2:
        cut[root] = True
res = [str(u) for u in range(1, n + 1) if cut[u]]
print(" ".join(res) if res else "NONE")

Java solution

import java.io.*;
import java.util.*;

public class Main {
    static List<List<Integer>> adj = new ArrayList<>();
    static int[] tin, low;
    static boolean[] cut;
    static int timer = 0;

    static void dfs(int u, int p) {
        tin[u] = low[u] = timer++;
        int children = 0;
        for (int v : adj.get(u)) {
            if (v == p) continue;
            if (tin[v] >= 0) low[u] = Math.min(low[u], tin[v]);
            else {
                dfs(v, u);
                low[u] = Math.min(low[u], low[v]);
                if (p != 0 && low[v] >= tin[u]) cut[u] = true;
                children++;
            }
        }
        if (p == 0 && children >= 2) cut[u] = true;
    }

    public static void main(String[] args) throws Exception {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int m = (int) in.nval;
        for (int i = 0; i <= n; i++) adj.add(new ArrayList<>());
        for (int i = 0; i < m; i++) {
            in.nextToken();
            int u = (int) in.nval;
            in.nextToken();
            int v = (int) in.nval;
            adj.get(u).add(v);
            adj.get(v).add(u);
        }
        tin = new int[n + 1];
        low = new int[n + 1];
        cut = new boolean[n + 1];
        Arrays.fill(tin, -1);
        // a deep path needs a bigger stack than the default thread gives
        Thread t = new Thread(null, () -> {
            for (int u = 1; u <= n; u++)
                if (tin[u] < 0) dfs(u, 0);
        }, "dfs", 1 << 26);
        t.start();
        t.join();
        StringBuilder sb = new StringBuilder();
        for (int u = 1; u <= n; u++)
            if (cut[u]) {
                if (sb.length() > 0) sb.append(' ');
                sb.append(u);
            }
        System.out.println(sb.length() == 0 ? "NONE" : sb.toString());
    }
}

Solve Articulation Points online · All practice problems