Cod sursa(job #956914)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 4 iunie 2013 00:41:57
Problema Componente biconexe Scor 8
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int nmax = 100010;
const int mmax = 200010;

int N, M, d[nmax], m[nmax], p[nmax], viz[nmax], comp, timet, bucket[mmax],bcc[mmax], Com;
vector<int> Vertex[nmax];
pair<int,int> Edge[mmax];

int Neighbour(int edge, int from) {
    int next;
    if(Edge[edge].first == from)
        next = Edge[edge].second;
    else next = Edge[edge].first;
    return next;
}

int DFS(int node) {
    if(viz[node]) return m[node];

    d[node] = ++timet;
    m[node] = d[node];
    viz[node] = true;

    int down = M + 1;
    for(vector<int> :: iterator it = Vertex[node].begin(); it != Vertex[node].end(); it++) {
        if(bcc[*it] == 0) bcc[*it] = ++comp;

        int next = Neighbour(*it, node);

        if(!viz[next]) p[next] = *it;

        int nextm = DFS(next);

        if(node != 1) {
            int parent = Neighbour(p[node], node);
            if(d[parent] >= nextm)
                bcc[p[node]] = bcc[*it];
        }

        down = min(down, nextm);
    }


    m[node] = min(down, m[node]);
    return m[node];
}

int main()
{
    ifstream in ("biconex.in");
    ofstream out ("biconex.out");

    in >> N >> M;
    int i, a, b;
    for(i = 1; i <= M; i++) {
        in >> a >> b;
        Edge[i] = make_pair(a, b);
        Vertex[a].push_back(i);
        Vertex[b].push_back(i);
    }

    DFS(1);

    int Co = 0;
    for(i = 1; i <= M; i++){
        if(bucket[bcc[i]] == 0) Co++;
        bucket[bcc[i]]++;
    }
    out << Co;
    return 0;
}