Pagini recente » Cod sursa (job #1327952) | Cod sursa (job #298293) | Cod sursa (job #2910288) | Cod sursa (job #2413465) | Cod sursa (job #769156)
Cod sursa(job #769156)
#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, 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])
{
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];
}
void printComponents(int node)
{
list *p;
used[node] = 0;
stack[++top] = node;
for(p = G[node]; p; p = p->next)
if(used[p->v])
{
printComponents(p->v);
if(reach[p->v] >= level[node])
{
for(; stack[top] != node; --top)
fprintf(out, "%d ", stack[top]);
fprintf(out, "%d\n", stack[top]);
}
}
}
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;
DF(i);
}
fprintf(out, "%d\n", nrComponents);
for(i = 1; i<= N; ++i)
if(critical[i] && used[i])
{
top = -1;
printComponents(i);
}
fclose(in);
fclose(out);
return 0;
}