Skip to content
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
23 changes: 23 additions & 0 deletions SCC/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ public int[][] build()
計算量

追加した辺の本数を `m` として $O(n + m)$

### id

```java
public int id(int i)
```

頂点 `i` が (トポロジカル順序において) 何番目の強連結成分に属しているかを返します。即ち,`build` で得られる「頂点の配列」の配列を `g` とすると、`g[id(i)][j] = i` なる `j` が存在します。

__ただし、`build` を呼ぶ前にこのメソッドが呼ばれた場合は、実行時例外 `UnsupportedOperationException` が発生します。__

制約

- __`build` メソッドを既に呼んでいる__
- `0 <= i < n`

計算量

$O(1)$

## 使用例

[AtCoder Library Practice Contest G - SCC](https://atcoder.jp/contests/practice2/submissions/16603978)
14 changes: 13 additions & 1 deletion SCC/SCC.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@ public Edge(int from, int to) {
int m;
final java.util.ArrayList<Edge> unorderedEdges;
final int[] start;
final int[] ids;
boolean hasBuilt = false;

public SCC(int n) {
this.n = n;
this.unorderedEdges = new java.util.ArrayList<>();
this.start = new int[n + 1];
this.ids = new int[n];
}

public void addEdge(int from, int to) {
unorderedEdges.add(new Edge(from, to));
start[from + 1]++;
this.m++;
}

public int id(int i) {
if (!hasBuilt) {
throw new UnsupportedOperationException(
"Graph hasn't been built."
);
}
return ids[i];
}

public int[][] build() {
for (int i = 1; i <= n; i++) {
Expand All @@ -42,7 +54,6 @@ public int[][] build() {
int[] vis = new int[n];
int[] low = new int[n];
int[] ord = new int[n];
int[] ids = new int[n];
java.util.Arrays.fill(ord, -1);
// u = lower32(stack[i]) : visiting vertex
// j = upper32(stack[i]) : jth child
Expand Down Expand Up @@ -110,6 +121,7 @@ public int[][] build() {
int cmp = ids[i];
groups[cmp][--counts[cmp]] = i;
}
hasBuilt = true;
return groups;
}
}