Cod sursa(job #575793)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 8 aprilie 2011 19:02:54
Problema Componente biconexe Scor 12
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>

using namespace std;

#define N 100000
#define M 200000

vector<int> g[N];
stack<pair<int, int> > sm;
int n, m;
int desc[100], low[100];
int t = 0;
int ncbc = 0;

ofstream fo("biconex.out");

void citeste()
{
	ifstream fi("biconex.in");
	fi >> n >> m;
	int x, y;
	for(int i = 0; i < m; ++i)
	{
		fi >> x >> y;
		x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	fi.close();
}

void dfs(int v, int tata)
{
	desc[v] = t; // timp de descoperire
	low[v] = t; // lowest desc
	t++;
	for(vector<int> :: iterator it = g[v].begin(); it != g[v].end(); ++it)
	{
		int u = *it;
		if(tata == u) continue;
		sm.push(pair<int, int>(v, u));
		if(desc[u] < 0)
		{
			dfs(u, v);
			if(low[v] > low[u]) low[v] = low[u];
			if(low[u] >= desc[v])
			{
				ncbc++;
				while(1)
				{
					pair<int, int> arc = sm.top();
					sm.pop();
					// cout << "(" << arc.first << ", " << arc.second << ") ";
					if(arc == pair<int, int>(v, u)) 
					{
						// cout << endl;
						break;
					}
				}
			}
		}
		else
		{
			if(low[v] > desc[u]) low[v] = desc[u];
		}
	}
}

int main()
{
	citeste();

	for(int i = 0; i < n; ++i) desc[i] = -1;
	dfs(0, -1);
	
	fo << ncbc << endl;

	fo.close();
	return 0;
}