[URL]
https://www.acmicpc.net/problem/13023
13023번: ABCDE
문제의 조건에 맞는 A, B, C, D, E가 존재하면 1을 없으면 0을 출력한다.
www.acmicpc.net
[풀이 과정]
*그래프 구현(벡터), 깊이우선탐색(DFS)
1. 배열로 깊이 우선 탐색 (시간 초과)
2. 그래프 깊이 우선 탐색
3. 연결관계 4개 찾으면 flag = 1로 바꿔주고
flag 값 검색하여 1이면 return;
[소스 코드]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define MAX 2005 int N, M; vector <int> graph[MAX]; bool check[MAX]; bool flag = 0; void DFS(int cur, int cnt) { if (cnt == 4) { flag = 1; return; } check[cur] = true; for (int i = 0; i < graph[cur].size(); i++) { int next = graph[cur][i]; if (check[next] == false) { DFS(next, cnt + 1); } if (flag == 1) { return; } } check[cur] = false; } int main() { scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { int a, b; scanf("%d %d", &a, &b); graph[a].push_back(b); graph[b].push_back(a); } for (int i = 0; i < N; i++) { memset(check, 0, sizeof(check)); if (flag == 1) break; DFS(i, 0); } printf("%d\n", flag); return 0; } | cs |
'Algorithm > 백준' 카테고리의 다른 글
17140. - 이차원 배열과 연산 (0) | 2019.07.16 |
---|---|
[BOJ] - 16236. 아기 상어 (0) | 2019.07.10 |
[BOJ] - 14502. 연구소 (0) | 2019.07.03 |
[BOJ] - 2156. 포도주 시식 (0) | 2019.07.01 |
[BOJ] - 17144. 미세먼지 안녕! (0) | 2019.06.25 |