From 9419785bc1548585a02e85db9cab0f16458aa9a8 Mon Sep 17 00:00:00 2001 From: wonkydd Date: Tue, 26 Mar 2024 16:54:55 +0900 Subject: [PATCH] [week-10] 1890, 9466 --- week10/wonkyDD/.gitkeep | 0 week10/wonkyDD/1890.cpp | 43 +++++++++++++++++++++++++++++++++++ week10/wonkyDD/9466.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) delete mode 100644 week10/wonkyDD/.gitkeep create mode 100644 week10/wonkyDD/1890.cpp create mode 100644 week10/wonkyDD/9466.cpp diff --git a/week10/wonkyDD/.gitkeep b/week10/wonkyDD/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/week10/wonkyDD/1890.cpp b/week10/wonkyDD/1890.cpp new file mode 100644 index 0000000..7edfaba --- /dev/null +++ b/week10/wonkyDD/1890.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +#define MAX 100 +using ll = long long; + +int map[MAX][MAX]; +ll dp[MAX][MAX]; +int N; + +int main() { + ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); + + cin >> N; + for (int i=0; i> map[i][j]; + } + } + memset(dp, 0, sizeof(dp)); + + /** NOTE : 시작점의 dp값을 1로 설정한다 */ + dp[0][0] = 1; + for (int y=0; y +#include +#define MAX 100001 +using namespace std; +int choice[MAX]; +bool visited[MAX]; +bool cycle[MAX]; +int n; +int cnt; + +void dfs(int cur) { + visited[cur] = true; + int next = choice[cur]; + if (visited[next]) { + // NOTE : 방문이 됐음에도 cycle로 마킹되지 않았어야만 사이클로 카운팅한다 + if (!cycle[next]) { + for (int i=next; i!=cur; i=choice[i]) ++cnt; + ++cnt; + } + } + else dfs(next); + + /** + * NOTE + * - 선택한 학생에 대한 dfs를 끝내고 나서야 cycle 마킹 + * - 위에있는 cnt를 세는 부분에서 flag로 사용된다 + */ + cycle[cur] = true; +} + +int main() { + ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); + + int T; cin >> T; + while (T--) { + cin >> n; + cnt = 0; + + for (int i=1; i<=n; ++i) { + cin >> choice[i]; + visited[i] = cycle[i] = false; + } + + for (int i=1; i<=n; ++i) { + if (!visited[i]) dfs(i); + } + cout << n-cnt << '\n'; + } + return 0; +} \ No newline at end of file