From dd85b22ffb38793925e6750aa3860066f04474b2 Mon Sep 17 00:00:00 2001 From: oncsr Date: Tue, 18 Feb 2025 13:06:02 +0900 Subject: [PATCH] =?UTF-8?q?[20250218]=20BOJ=20/=20G4=20/=20=ED=82=A4=20?= =?UTF-8?q?=EC=88=9C=EC=84=9C=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \355\202\244 \354\210\234\354\204\234.md" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "khj20006/202502/18 BOJ G4 \355\202\244 \354\210\234\354\204\234.md" diff --git "a/khj20006/202502/18 BOJ G4 \355\202\244 \354\210\234\354\204\234.md" "b/khj20006/202502/18 BOJ G4 \355\202\244 \354\210\234\354\204\234.md" new file mode 100644 index 00000000..1f3ed4ed --- /dev/null +++ "b/khj20006/202502/18 BOJ G4 \355\202\244 \354\210\234\354\204\234.md" @@ -0,0 +1,80 @@ +### 플로이드 + +```cpp + +#include +#include +using namespace std; +int main() +{ + int N, M; + cin >> N >> M; + vector> D(N + 1, vector(N + 1)); + for (int i = 1; i <= N; i++) D[i][i] = 1; + for (int i = 0, a, b; i < M; i++) { + cin >> a >> b; + D[a][b] = 1; + } + + for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k++) { + if (D[j][i] && D[i][k]) D[j][k] = 1; + } + + int ans = 0; + for (int i = 1; i <= N; i++) { + int res = 0; + for (int j = 1; j <= N; j++) res += D[i][j] + D[j][i]; + ans += res == N + 1; + } + cout << ans; + +} + +``` + +### 위상 정렬 + +```cpp + +#include +#include +#include +using namespace std; + +int main() +{ + int N, M; + cin >> N >> M; + vector> V(N + 1); + vector> D(N + 1, vector(N + 1)); + vector in(N + 1); + for (int i = 1; i <= N; i++) D[i][i] = 1; + for (int i = 0, a, b; i < M; i++) { + cin >> a >> b; + V[a].push_back(b); + in[b]++; + } + + queue Q; + for (int i = 1; i <= N; i++) if (!in[i]) Q.push(i); + + while (!Q.empty()) { + int x = Q.front(); + Q.pop(); + for (int i : V[x]) { + for (int j = 1; j <= N; j++) if (D[j][x]) D[j][i] = 1; + if (!(--in[i])) Q.push(i); + } + } + + int ans = 0; + for (int i = 1; i <= N; i++) { + int res = 0; + for (int j = 1; j <= N; j++) res += D[i][j] + D[j][i]; + ans += res == N + 1; + } + cout << ans; + +} + +```