Cod sursa(job #769150)

Utilizator igsifvevc avb igsi Data 18 iulie 2012 14:26:46
Problema Componente biconexe Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 1.37 kb
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100001

struct list{ int v; struct list *next;};
typedef struct list list;

FILE *in, *out;
int root[SIZE], level[SIZE], reach[SIZE], stack[SIZE], top, N, treeRoot, nrTrees, nrComponents;
char used[SIZE], critical[SIZE];
list *G[SIZE];

void DF(int node)
{
	list *p;
	used[node] = 1;
	reach[node] = level[node];
	
	for(p = G[node]; p; p = p->next)
		if(!used[p->v])
		{
			if(node == treeRoot) nrTrees++;
			
			level[p->v] = reach[p->v] = level[node] + 1;
			DF(p->v);
			
			if(reach[p->v] < reach[node])
				reach[node] = reach[p->v];
			if(reach[p->v] >= level[node])
				critical[node] = 1,
				nrComponents++;
		}
		else
			if(p->v != root[node] && reach[node] > level[p->v])
				reach[node] = level[p->v];
}

int main()
{
	int M, a, b, i;
	list *p;
	
	in = fopen("biconex.in", "r");
	out = fopen("biconex.out", "w");
	
	fscanf(in, "%d %d", &N, &M);
	for(; M; --M)
	{
		fscanf(in, "%d %d", &a, &b);
		p = malloc(sizeof(list));
		p->v = b;
		p->next = G[a];
		G[a] = p;
		
		p = malloc(sizeof(list));
		p->v = a;
		p->next = G[b];
		G[b] = p;
	}
	
	for(i = 1; i <= N; ++i)
		if(!used[i])
		{
			level[i] = 1;
			treeRoot = i;
			nrTrees = 0;
			DF(i);
			
			critical[i] = nrTrees > 1;
			
		}
	
	fprintf(out, "%d\n", nrComponents);
	
	/*for(i = 1; i <= N; ++i)
		if(critical[i])
			printf("%d\n", i);
	*/
	fclose(in);
	fclose(out);
	return 0;
}