import java.io.*;
import java.util.*;
public class pipes {
static ArrayList<Integer>[] adjList;
static int[] dfs_low, dfs_num, parent;
static int V, counter;
static PrintWriter out = new PrintWriter(System.out);
static int [][]cnt;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
V = sc.nextInt();
cnt=new int [V][V];
adjList = new ArrayList[V];
for (int i = 0; i < V; i++)
adjList[i] = new ArrayList();
int m = sc.nextInt();
while (m-- > 0) {
int u = sc.nextInt() - 1, v = sc.nextInt() - 1;
adjList[u].add(v);
adjList[v].add(u);
cnt[u][v]++;
cnt[v][u]++;
}
dfs_low = new int[V];
dfs_num = new int[V];
parent = new int[V];
findArtPointsAndBridges();
out.close();
}
static void findArtPointsAndBridges() // O(V + E)
{
for (int i = 0; i < V; ++i)
if (dfs_num[i] == 0) {
dfs(i);
}
}
static void dfs(int u) {
dfs_num[u] = dfs_low[u] = ++counter;
for (int v : adjList[u])
if (dfs_num[v] == 0) {
parent[v] = u;
dfs(v);
if (dfs_low[v] > dfs_num[u] && cnt[u][v]==1)
out.printf("%d %d%n", u + 1, v + 1);
dfs_low[u] = Math.min(dfs_low[v], dfs_low[u]);
} else if (parent[u] != v)
dfs_low[u] = Math.min(dfs_low[u], dfs_num[v]);
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready();
}
}
}
Compilation message
Note: pipes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
10080 KB |
Output is correct |
2 |
Correct |
105 ms |
10228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
166 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
159 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
171 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
167 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
164 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
165 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |