-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyess.java
More file actions
94 lines (88 loc) · 2.35 KB
/
yess.java
File metadata and controls
94 lines (88 loc) · 2.35 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
class Edge implements Comparable <Edge> {
int first;
int second;
public Edge(int f, int s) {
this.first = f;
this.second = s;
}
@Override
public int compareTo(Edge e) {
if(this.first == e.first) {
return this.second - e.second;
}
return this.first- e.first;
}
}
public class Main{
// 단절선 문제
// dfs기반 order, low 를 구하기
static int v;
static int e;
static int [] order;
static ArrayList [] al ;
static int index = 1;
static boolean [] isit ;
static StringBuilder sb = new StringBuilder ();
static PriorityQueue<Edge> pq = new PriorityQueue<>();
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
isit = new boolean [ v + 1];
order = new int [v + 1];
al = new ArrayList [v + 1];
for(int i = 1; i <= v; i ++) {
al [i] = new ArrayList<>();
}
for(int i = 1; i <= e; i ++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
al[a].add(b);
al[b].add(a);
}
dfs(1,0);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
sb.append(pq.size() + "\n");
while(!pq.isEmpty()) {
sb.append(pq.peek().first + " " + pq.peek().second);
pq.poll();
sb.append("\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
static int dfs(int now, int parent) {
order[now] = index ++;
int ret = order[now];
int size = al[now].size();
for(int i = 0 ; i < size; i ++) {
int next = (int) al[now].get(i);
if(next == parent) continue;
if(order[next] == 0 ) {
int low = dfs(next, now);
if( low > order[now] ) {
int first = Math.min(next, now);
int second = Math.max(next, now);
pq.add(new Edge(first, second));
}
ret = Math.min(low, ret);
}
else {
ret = Math.min(order[next], ret);
}
}
return ret;
}
}