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 | #include <cstdio> #include <vector> #include <iostream> using namespace std; vector<int> array[2000]; bool visited[2000]; bool dfs(int depth, int n) { if(depth == 5) { // 정답조건 return true; } visited[n] = true; //방문 체크 for(int i = 0; i < array[n].size(); i++) { //다음 노드로 이동 int next = array[n][i]; //다음 노드 if(visited[next]) continue; //방문 했으면 다음노드 보기 if(dfs(depth + 1, next)) { return true; } } visited[n] = false; return false; } int main(void) { int depth, N, M, a, b; scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d %d", &a, &b); array[a].push_back(b); array[b].push_back(a); } for(int i = 0 ; i < N; i++) { // 시작위치를 0 ~ N-1 까지 모든 경우를 다해봄. if(dfs(1, i)) { printf("1"); return 0; } } printf("0"); } | cs |
'알고리즘' 카테고리의 다른 글
백준(BOJ) 10250 - ACM 호텔 (0) | 2019.01.02 |
---|---|
백준(BOJ) 2293 - 동전1 (0) | 2019.01.02 |
알고스팟(algospot) ITES - 외계 신호 분석 (0) | 2018.12.05 |
백준(BOJ) 2581 소수 (0) | 2018.11.29 |
백준(BOJ) 1316 그룹 단어 체커 (0) | 2018.11.29 |