My issue
The output is displayed but the hidden case is failed
My code
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES 100
int visited[MAX_NODES];
void dfs(int node, int graph[][MAX_NODES]) {
visited[node] = 1;
printf("%d ", node);
for (int i = 1; i <= MAX_NODES; i++) {
if (!visited[i] && graph[node][i] == 1) {
dfs(i, graph);
}
}
}
int main() {
int N;
scanf("%d", &N);
int graph[MAX_NODES][MAX_NODES] = {{0}};
for (int i = 0; i < N - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1; // Assuming undirected graph
}
dfs(1, graph);
return 0;
}
Learning course: Data Structures & Algorithms using C
Problem Link: https://www.codechef.com/learn/course/ciet-data-structures-c/CIETDSC22/problems/DSADFS