#include <stdio.h>
#include <stdlib.h>
typedef struct vertex {
int name;
struct vertex** neighbors;
int numNeighbors;
// int* weights;
} vertex;
typedef struct graphVertex {
int numNodes;
int numEdges;
vertex* nodes;
} graphVertex;
void dfs_pointers(graphVertex graph, int currentNode, int* visited)
{
if (!visited[currentNode]) {
//printf("%d ", currentNode);
visited[currentNode] = 1;
for (int i = 0; i < graph.nodes[currentNode].numNeighbors; i++)
if (!visited[graph.nodes[currentNode].neighbors[i]->name])
dfs_pointers(graph, graph.nodes[currentNode].neighbors[i]->name, visited);
}
}
graphVertex readGraphVertex(const char* fileName)
{
graphVertex graph;
graph.numEdges = 0;
graph.numNodes = 0;
FILE* f = fopen(fileName, "r");
if (f == NULL)
return graph;
fscanf(f, "%i%i", &(graph.numNodes), &(graph.numEdges));
graph.nodes = (vertex*)malloc(sizeof(vertex) * (graph.numNodes + 1));
if (graph.nodes == NULL)
return graph;
for (int i = 0; i <= graph.numNodes; i++) {
graph.nodes[i].name = i;
graph.nodes[i].numNeighbors = 0;
graph.nodes[i].neighbors = NULL;
}
for (int i = 0; i < graph.numEdges; i++) {
int x, y;
fscanf(f, "%i%i", &x, &y);
graph.nodes[x].numNeighbors++;
graph.nodes[x].neighbors = (vertex**)realloc(graph.nodes[x].neighbors, graph.nodes[x].numNeighbors * sizeof(vertex*));
graph.nodes[x].neighbors[graph.nodes[x].numNeighbors - 1] = &(graph.nodes[y]);
graph.nodes[y].numNeighbors++;
graph.nodes[y].neighbors = (vertex**)realloc(graph.nodes[y].neighbors, graph.nodes[y].numNeighbors * sizeof(vertex*));
graph.nodes[y].neighbors[graph.nodes[y].numNeighbors - 1] = &(graph.nodes[x]);
}
fclose(f);
return graph;
}
int main()
{
graphVertex graphV = readGraphVertex("dfs.in");
/*for (int i = 1; i <= graphV.numNodes; i++) {
printf("%d ", graphV.nodes[i].numNeighbors);
}*/
int* visited = (int*)malloc(sizeof(int) * (graphV.numNodes + 1));
int graphs = 0;
//memset(visited, 0, graphV.numNodes);
for (int i = 1; i <= graphV.numNodes; i++) visited[i] = 0;
for (int i = 1; i <= graphV.numNodes; i++)
if (!visited[i]) {
graphs++;
dfs_pointers(graphV, i, visited);
}
FILE* g = fopen("dfs.out", "w");
fprintf(g, "%d", graphs);
fclose(g);
return 0;
}
/*int visited[100001];
int n, e;
int main()
{
FILE* f = fopen("dfs.in", "r");
fscanf(f, "%d %d", &n, &e);
int numberGraphs = 0, color = 0;
int x, y;
for (int i = 0; i < e; i++) {
fscanf(f, "%d %d", &x, &y);
if (!visited[x] && !visited[y]) {
numberGraphs++;
color++;
visited[x] = color;
visited[y] = color;
} else if (!visited[x])
visited[x] = visited[y];
else if (!visited[y])
visited[y] = visited[x];
else {
//to do
if (visited[x] != visited[y]) {
numberGraphs--;
int aux = visited[x];
for (int j = 1; j <= n; j++)
if (visited[j] == aux)
visited[j] = visited[y];
//to do parcurg si schimb culoarea
}
//else aceeasi culoare deci nimic
}
}
for (int i = 1; i <= n; i++)
if (!visited[i]) numberGraphs++;
FILE* g = fopen("dfs.out", "w");
fprintf(g, "%d", numberGraphs);
fclose(f);
fclose(g);
}
*/
/*int n, e;
int visited[100001];
typedef struct edge {
int leftnode;
int rightnode;
} edge;
void dfs(int currentNode, int graphColor, edge* edges, int* visited)
{
if (!visited[currentNode]) {
visited[currentNode] = graphColor;
for (int i = 0; i < e; i++) {
if (edges[i].leftnode == currentNode)
dfs(edges[i].rightnode, graphColor, edges, visited);
if (edges[i].rightnode == currentNode)
dfs(edges[i].leftnode, graphColor, edges, visited);
}
}
}
int main()
{
FILE* f = fopen("dfs.in", "r");
fscanf(f, "%d %d", &n, &e);
int numberGraphs = 0;
edge* edges = (edge*)malloc(e * sizeof(edge));
for (int i = 0; i < e; i++)
fscanf(f, "%d %d", &edges[i].leftnode, &edges[i].rightnode);
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
numberGraphs++;
dfs(i, numberGraphs, edges, visited);
}
}
FILE* g = fopen("dfs.out", "w");
fprintf(g, "%d", numberGraphs);
fclose(f);
fclose(g);
/*for (int i = 1; i <= n; i++)
printf("%d ", visited[i]);
}
*/