Cod sursa(job #3378890)

Utilizator fmi-studentnu sunt de acord fmi-student Data 28 noiembrie 2026 18:51:13
Problema Componente biconexe Scor 100
Compilator rs Status done
Runda Arhiva educationala Marime 3.14 kb
use std::fs;

fn main() {
    let input = fs::read_to_string("biconex.in").expect("");
    let mut tokens = input.split_whitespace();

    let n: usize = tokens.next().unwrap().parse().unwrap();
    let m: usize = tokens.next().unwrap().parse().unwrap();

    let mut graph: Vec<Vec<(usize, usize)>> = vec![Vec::new(); n + 1];
    let mut edges: Vec<(usize, usize)> = Vec::with_capacity(m);

    for id in 0..m {
        let u: usize = tokens.next().unwrap().parse().unwrap();
        let v: usize = tokens.next().unwrap().parse().unwrap();

        graph[u].push((v, id));
        graph[v].push((u, id));
        edges.push((u, v));
    }

    let mut tin = vec![0_usize; n + 1];
    let mut low = vec![0_usize; n + 1];

    let mut parent = vec![0_usize; n + 1];
    let mut parent_edge = vec![usize::MAX; n + 1];
    let mut next = vec![0_usize; n + 1];

    let mut stack: Vec<usize> = Vec::with_capacity(n);
    let mut edge_stack: Vec<usize> = Vec::with_capacity(m);

    let mut components: Vec<Vec<usize>> = Vec::new();
    let mut seen = vec![0_usize; n + 1];

    let mut time = 0;

    for start in 1..=n {
        if tin[start] != 0 {
            continue;
        }

        time += 1;
        tin[start] = time;
        low[start] = time;
        stack.push(start);

        while let Some(&u) = stack.last() {
            if next[u] < graph[u].len() {
                let (v, id) = graph[u][next[u]];
                next[u] += 1;

                if id == parent_edge[u] {
                    continue;
                }

                if tin[v] == 0 {
                    parent[v] = u;
                    parent_edge[v] = id;

                    edge_stack.push(id);

                    time += 1;
                    tin[v] = time;
                    low[v] = time;

                    stack.push(v);
                } else if tin[v] < tin[u] {
                    low[u] = low[u].min(tin[v]);
                    edge_stack.push(id);
                }
            } else {
                stack.pop();

                let p = parent[u];

                if p == 0 {
                    continue;
                }

                low[p] = low[p].min(low[u]);

                if low[u] >= tin[p] {
                    let mut component = Vec::new();
                    let mark = components.len() + 1;

                    loop {
                        let id = edge_stack.pop().unwrap();
                        let (a, b) = edges[id];

                        for v in [a, b] {
                            if seen[v] != mark {
                                seen[v] = mark;
                                component.push(v);
                            }
                        }

                        if id == parent_edge[u] {
                            break;
                        }
                    }

                    components.push(component);
                }
            }
        }
    }

    let mut output = format!("{}\n", components.len());

    for component in components {
        for u in component {
            output.push_str(&format!("{} ", u));
        }

        output.push('\n');
    }

    fs::write("biconex.out", output).expect("");
}