https://www.acmicpc.net/problem/11724

Baekjoon Online JudgeBaekjoon Online Judgewww.acmicpc.net

이문제고 


#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <math.h>


int arr[1001][1001];

int N,M;

int check[1001];



void DFS(int a)

{

check[a] = 1;

for (int i = 1; i <= N; i++)

{

if (arr[a][i] == 1 && check[i]==0)

{

DFS(i);

}

}

}



int main(void)

{

scanf("%d %d", &N, &M);

int a, b;

if (N == 1 && M == 0)

{

printf("1");

return 0;

}

int count = 0;

for (int i = 0; i < M; i++)

{

scanf("%d %d", &a, &b);

arr[a][b] = 1;

arr[b][a] = 1;

}

for (int i = 1; i <= N; i++)

{

for (int j = 1; j <= N; j++)

{

if (arr[i][j] == 1 && check[i]==0&&check[j]==0)

{

count++;

DFS(i);

DFS(j);

}

}

}

printf("%d", count);

}


이렇게 풀었는데 채점중40%에서 틀렸습니다가 나오네 자꾸.... 반례가 뭐있길래 틀린건지 아는 분