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%에서 틀렸습니다가 나오네 자꾸.... 반례가 뭐있길래 틀린건지 아는 분
N=2 M=0일때 0나올것 같음
근데 정점개수가 2개이상이면 연결요소가 한개도 없는거 아님?
간선이 없어도 자기자신 혼자서 연결요소로 봐야될거야
아 그러게 그렇게 하니까 풀림 고마워 문제이해를 못했었네