Skip to content

Commit ee794a2

Browse files
committed
SCC: index チェックを追加
1 parent 2ec93f4 commit ee794a2

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎SCC/SCC.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public SCC(int n) {
2525
}
2626

2727
public void addEdge(int from, int to) {
28+
rangeCheck(from);
29+
rangeCheck(to);
2830
unorderedEdges.add(new Edge(from, to));
2931
start[from + 1]++;
3032
this.m++;
@@ -36,6 +38,7 @@ public int id(int i) {
3638
"Graph hasn't been built."
3739
);
3840
}
41+
rangeCheck(i);
3942
return ids[i];
4043
}
4144

@@ -127,4 +130,12 @@ public int[][] build() {
127130
hasBuilt = true;
128131
return groups;
129132
}
133+
134+
private void rangeCheck(int i) {
135+
if (i < 0 || i >= n) {
136+
throw new IndexOutOfBoundsException(
137+
String.format("Index %d out of bounds for length %d", i, n)
138+
);
139+
}
140+
}
130141
}

0 commit comments

Comments
 (0)