diff --git a/InSange/DFS/2668.cpp b/InSange/DFS/2668.cpp new file mode 100644 index 0000000..e6ea5a5 --- /dev/null +++ b/InSange/DFS/2668.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +int N, num; +vector visited; +vector v; +vector answer; + +void DFS(int cur, int start) +{ + if (visited[cur] == true) + { + if (start == cur) answer.push_back(start); + return; + } + + visited[cur] = true; + DFS(v[cur], start); + visited[cur] = false; +} + +void Solve() +{ + cin >> N; + + N += 1; + visited.assign(N, false); + v.assign(N, 0); + + for (int i = 1; i < N; i++) + { + cin >> v[i]; + } + + for (int i = 1; i < N; i++) + { + DFS(i, i); + } + + int asize = answer.size(); + cout << asize << "\n"; + + for (int i = 0; i < asize; i++) + { + cout << answer[i] << "\n"; + } +} + +int main() +{ + cin.tie(nullptr); + ios::sync_with_stdio(false); + + Solve(); + + return 0; +} \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index 358e16c..a29d500 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -12,4 +12,5 @@ | 8차시 | 2024.04.09 | 투 포인터 | [두 용액](https://www.acmicpc.net/problem/2470) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/32)] | 9차시 | 2024.04.12 | 힙 | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)] | 10차시 | 2024.05.02 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)] +| 11차시 | 2024.05.02 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41)] ---