https://www.acmicpc.net/problem/11400
11400번: 단절선
첫째 줄에 두 정수 V(1≤V≤100,000), E(1≤E≤1,000,000)가 주어진다. 이는 그래프가 V개의 정점과 E개의 간선으로 이루어져 있다는 의미이다. 다음 E개의 줄에는 간선에 대한 정보를 나타내는 두 정수 A
www.acmicpc.net
문제
https://katastrophe.tistory.com/70 이전 포스팅 참고 ( 단절점 )
풀이
단절점 찾기랑 원리가 같다.
다른점은 DFS를 진행할때 부모노드를 기록해 나가는 것.
만약 정점 A의 자식노드보다 더 낮은 값이 DFS결과로서 리턴이 되면 우회로가 존재
그렇지 않다면 해당 노드는 단절점이므로 , 이 단절점의 부모노드와의 Edge를 list에 넣고 정렬하면 끝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Main {
static int V,E,order=1;
static int[] discover;
static ArrayList<Integer>[] list;
static ArrayList<int[]> cutEdge = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
V = input[0]; E = input[1];
list = new ArrayList[V+1];
discover = new int[V+1];
for(int i=1;i<=V;i++) list[i] = new ArrayList<>();
for(int i=1;i<=E;i++) {
input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
list[input[0]].add(input[1]); list[input[1]].add(input[0]);
}
for(int i=1;i<=V;i++)
if(discover[i]==0) dfs(i,0);
cutEdge.sort((o1, o2) -> o1[0]!=o2[0] ? o1[0]-o2[0] : o1[1]-o2[1]);
System.out.println(cutEdge.size());
for (int[] edge : cutEdge)
System.out.println(edge[0]+" "+edge[1]);
}
private static int dfs(int cur,int parent) {
discover[cur] = order++;
int ret = discover[cur];
for (int next : list[cur]) {
if(next==parent) continue;
if(discover[next]==0) {
int prev = dfs(next,cur);
if(prev > discover[cur])
cutEdge.add(new int[]{Math.min(cur,next), Math.max(cur,next)});
ret = Math.min(ret,prev);
}else ret = Math.min(ret,discover[next]);
}
return ret;
}
}
|
cs |
'알고리즘,PS > 백준' 카테고리의 다른 글
[BOJ] 백준 [1781] 컵라면 JAVA (0) | 2021.11.02 |
---|---|
[BOJ] 백준 [1461] 도서관 JAVA (0) | 2021.11.01 |
[BOJ] 백준 [11266] 단절점 JAVA (0) | 2021.10.31 |
[BOJ] 백준 [11497] 통나무 건너뛰기JAVA (0) | 2021.10.30 |
[BOJ] 백준 [13904] 과제JAVA (0) | 2021.10.29 |