Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7-Me-in-U #22

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BOJ-ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
| 3μ°¨μ‹œ | 2024.03.26 | νŽœμœ…νŠΈλ¦¬ | [ꡬ간 ν•© κ΅¬ν•˜κΈ°](https://www.acmicpc.net/problem/2042) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/13) |
| 4μ°¨μ‹œ | 2024.03.30 | 볼둝껍질 | [볼둝 껍질](https://www.acmicpc.net/problem/1708) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/14) |
| 5μ°¨μ‹œ | 2024.04.29 | μŠ€μœ„ν•‘ | [μ—¬λŸ¬ μ§μ‚¬κ°ν˜•μ˜ 전체 면적 κ΅¬ν•˜κΈ°](https://www.acmicpc.net/problem/2672) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/19) |
| 6μ°¨μ‹œ | 2024.05.13 | μœ„μƒμ •λ ¬ | [쀄 μ„Έμš°κΈ°](https://www.acmicpc.net/problem/2252) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/20) |
| 6μ°¨μ‹œ | 2024.05.13 | μœ„μƒμ •λ ¬ | [쀄 μ„Έμš°κΈ°](https://www.acmicpc.net/problem/2252) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/20) |
| 7μ°¨μ‹œ | 2024.05.18 | SCC | [Strongly Connected Component](https://www.acmicpc.net/problem/2150) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/22) |
92 changes: 92 additions & 0 deletions BOJ-ios/SCC/P2150번_Strongly_Connected_Component/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package Platinum_V.P2150번_Strongly_Connected_Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {
protected static ArrayList<Integer>[] graph;
protected static ArrayList<Integer>[] reversedGraph;
protected static ArrayList<Integer>[] sccGraph;
protected static Deque<Integer> stack;
protected static boolean[] visited;

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

// μž…λ ₯ 및 μ΄ˆκΈ°ν™”
StringTokenizer st = new StringTokenizer(br.readLine());
int V = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
graph = new ArrayList[V + 1];
reversedGraph = new ArrayList[V + 1];
sccGraph = new ArrayList[V + 1];
stack = new ArrayDeque<>();
visited = new boolean[V + 1];
for (int i = 1; i <= V; i++) {
graph[i] = new ArrayList<>();
reversedGraph[i] = new ArrayList<>();
sccGraph[i] = new ArrayList<>();
}
for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());
graph[v1].add(v2);
reversedGraph[v2].add(v1);
}

// 정상 κ·Έλž˜ν”„μ—μ„œ DFS
for (int i = 1; i <= V; i++)
if (!visited[i])
DFS(i);

// μ—­λ°©ν–₯ κ·Έλž˜ν”„μ—μ„œ DFS
visited = new boolean[V + 1];
int sccCount = 0;
while (!stack.isEmpty()) {
int vertex = stack.pop();
if (!visited[vertex]) {
reverseDFS(vertex, ++sccCount);
}
}
sb.append(sccCount).append('\n');

// μ •λ ¬
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
for (int i = 1; i <= sccCount; i++) {
sccGraph[i].sort((o1, o2) -> o1 - o2);
map.put(sccGraph[i].get(0), sccGraph[i]);
}
map.entrySet().iterator().forEachRemaining(e -> {
e.getValue().forEach(v -> sb.append(v).append(' '));
sb.append(-1).append('\n');
});

// 좜λ ₯
System.out.print(sb.toString());
}

protected static void DFS(int currentVertex) {
visited[currentVertex] = true;
for (int nextVertex : graph[currentVertex])
if (!visited[nextVertex])
DFS(nextVertex);
stack.push(currentVertex);
}

protected static void reverseDFS(int currentVertex, int sccCount) {
visited[currentVertex] = true;
for (int nextVertex : reversedGraph[currentVertex])
if (!visited[nextVertex])
reverseDFS(nextVertex, sccCount);
sccGraph[sccCount].add(currentVertex);
}
}