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
| #include <cstdio> #include <cstring> #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define M 4000 #define N 1001
struct edge { int v, next; } E[M]; int adj[N]; int low[N], visited[N]; int step; inline int read() { int n = 0; char c; while ((c = getchar()) >= '0' && c <= '9') n = n * 10 + c - '0'; return n; } void DFS(int p, int u) { visited[u] = low[u] = ++step;
for (int i = adj[u]; i != -1; i = E[i].next) { int v = E[i].v; if (!visited[v]) { printf("%d %d\n", u, v); DFS(u, v); low[u] = MIN(low[u], low[v]); if (low[v] > visited[u]) printf("%d %d\n", v, u); } else if (v != p) { if (visited[v] < visited[u]) printf("%d %d\n", u, v);
low[u] = MIN(visited[v], low[u]); } } } int main() { int n, m, Case = 0; while (scanf("%d%d ", &n, &m) && (n || m)) { int u, v, idx = 0; memset(adj, -1, sizeof adj); for (int i = 0; i < m; ++i) { u = read(); v = read(); E[i << 1] = (edge){v, adj[u]}; adj[u] = idx++; E[(i << 1) + 1] = (edge){u, adj[v]}; adj[v] = idx++; }
printf("%d\n\n", ++Case);
for (int i = 1; i <= n; ++i) if (!visited[i]) DFS(-1, i); puts("#");
memset(visited, 0, sizeof visited); memset(low, 0, sizeof low); step = 0; }
return 0; }
|