File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed
Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ ### 플로이드
2+
3+ ``` cpp
4+
5+ #include < iostream>
6+ #include < vector>
7+ using namespace std ;
8+ int main ()
9+ {
10+ int N, M;
11+ cin >> N >> M;
12+ vector<vector<bool>> D(N + 1, vector<bool>(N + 1));
13+ for (int i = 1; i <= N; i++) D[i][i] = 1;
14+ for (int i = 0, a, b; i < M; i++) {
15+ cin >> a >> b;
16+ D[a][b] = 1;
17+ }
18+
19+ for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k++) {
20+ if (D[j][i] && D[i][k]) D[j][k] = 1;
21+ }
22+
23+ int ans = 0;
24+ for (int i = 1; i <= N; i++) {
25+ int res = 0;
26+ for (int j = 1; j <= N; j++) res += D[i][j] + D[j][i];
27+ ans += res == N + 1;
28+ }
29+ cout << ans;
30+
31+ }
32+
33+ ```
34+
35+ ### 위상 정렬
36+
37+ ``` cpp
38+
39+ #include < iostream>
40+ #include < vector>
41+ #include < queue>
42+ using namespace std ;
43+
44+ int main ()
45+ {
46+ int N, M;
47+ cin >> N >> M;
48+ vector<vector<int>> V(N + 1);
49+ vector<vector<bool>> D(N + 1, vector<bool>(N + 1));
50+ vector<int> in(N + 1);
51+ for (int i = 1; i <= N; i++) D[i][i] = 1;
52+ for (int i = 0, a, b; i < M; i++) {
53+ cin >> a >> b;
54+ V[a].push_back(b);
55+ in[b]++;
56+ }
57+
58+ queue<int> Q;
59+ for (int i = 1; i <= N; i++) if (!in[i]) Q.push(i);
60+
61+ while (!Q.empty()) {
62+ int x = Q.front();
63+ Q.pop();
64+ for (int i : V[x]) {
65+ for (int j = 1; j <= N; j++) if (D[j][x]) D[j][i] = 1;
66+ if (!(--in[i])) Q.push(i);
67+ }
68+ }
69+
70+ int ans = 0;
71+ for (int i = 1; i <= N; i++) {
72+ int res = 0;
73+ for (int j = 1; j <= N; j++) res += D[i][j] + D[j][i];
74+ ans += res == N + 1;
75+ }
76+ cout << ans;
77+
78+ }
79+
80+ ```
You can’t perform that action at this time.
0 commit comments