Pagini recente » Cod sursa (job #1377672) | Cod sursa (job #2848460) | Cod sursa (job #1566781) | Cod sursa (job #94592) | Cod sursa (job #2741291)
#include <stdio.h>
#include <stdlib.h>
typedef struct vertex_node {
int value;
struct vertex_node *next;
} vertex_node;
typedef struct graphVertex {
int numNodes;
int numEdges;
vertex_node *nodes[100001];
} graphVertex;
graphVertex graf;
void legare_noduri_lista(vertex_node **listStart, int value)
{
vertex_node *nou;
nou = malloc(sizeof(vertex_node *));
nou->value = value;
nou->next = *listStart;
*listStart = nou;
}
void read_graphVertex()
{
int dim1, dim2;
int nod1, nod2;
FILE *fin;
fin = fopen("dfs.in", "r");
fscanf(fin, "%d %d", &dim1, &dim2);
graf.numNodes = dim1;
graf.numEdges = dim2;
for (int i = 0; i < graf.numEdges; i++) {
fscanf(fin, "%d %d", &nod1, &nod2);
legare_noduri_lista(&graf.nodes[nod1], nod2);
legare_noduri_lista(&graf.nodes[nod2], nod1);
}
fclose(fin);
}
void marcheaza_DFS(int nod_curent, int *vizitat)
{
vertex_node *listStart;
vizitat[nod_curent] = 1;
for (listStart = graf.nodes[nod_curent]; listStart != NULL; listStart = listStart->next)
if (!vizitat[listStart->value])
marcheaza_DFS(listStart->value, vizitat);
}
void numara_conexe(int *vizitat, int *contor)
{
for (int i = 0; i < graf.numNodes; i++)
if (!vizitat[i]) {
(*contor)++;
marcheaza_DFS(i, vizitat);
}
}
int main()
{
FILE *fout;
int vizitat[100001];
read_graphVertex();
int contor = 0;
numara_conexe(vizitat, &contor);
fout = fopen("dfs.out", "w");
fprintf(fout, "%d", contor);
fclose(fout);
return 0;
}