-
[BOJ] 2668번 - 숫자고르기알고리즘 문제/BOJ 2022. 4. 19. 01:13
https://www.acmicpc.net/problem/2668
2668번: 숫자고르기
세로 두 줄, 가로로 N개의 칸으로 이루어진 표가 있다. 첫째 줄의 각 칸에는 정수 1, 2, …, N이 차례대로 들어 있고 둘째 줄의 각 칸에는 1이상 N이하인 정수가 들어 있다. 첫째 줄에서 숫자를 적절
www.acmicpc.net
sol) dfs
#include <iostream> #include <algorithm> #include <vector> #include <set> #include <cstring> using namespace std; int v[101]; int n; int ans; int check[200]; set <int> s; void dfs(int depth, int start, int a) { while (1) { if (v[depth] != -1 && v[depth] == start) { for (int i = 1; i <= n; i++) { if (check[i] == a) { s.insert(i); v[i] = -1; } } break; } if (v[depth] != -1 && check[v[depth]] != a) { check[v[depth]] = a; depth = v[depth]; } else break; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); int a = 1; cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= n; i++) { check[i] = a; dfs(i, i, a); a++; } cout << s.size() << '\n'; for (auto i : s) cout << i << '\n'; }'알고리즘 문제 > BOJ' 카테고리의 다른 글
[BOJ] 3075번 - Astromeeting (0) 2022.04.29 [ BOJ] 13460번 - 구슬 탈출 2 (0) 2022.04.28 [BOJ] 11559번 - Puyo Puyo (0) 2022.04.18 [BOJ] 1992번 - 쿼드트리 (0) 2022.04.10 [BOJ] 11967번 - 불켜기 (0) 2022.04.04