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
| #include <bits/stdc++.h>
using namespace std;
const int N = 150 + 5; const int M = 5000 + 5;
int head[N], nxt[M << 1], to[M << 1], tot; inline void addedge(int u, int v) { nxt[tot] = head[u]; to[tot] = v; head[u] = tot++; }
int edcc_num, edcc[N]; vector<pair<int, int>> cutEdge; int dfn[N], low[N], Index; stack<int> S; void Tarjan(int u, int lst) { dfn[u] = low[u] = ++Index; S.push(u); for (int i = head[u]; i != -1; i = nxt[i]) { if (i == (lst ^ 1)) continue; int v = to[i]; if (!dfn[v]) { Tarjan(v, i); low[u] = min(low[u], low[v]); if (low[v] > dfn[u]) cutEdge.push_back(make_pair(min(u, v), max(u, v))); } else low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { ++edcc_num; int v = -1; do { v = S.top(); S.pop(); edcc[v] = edcc_num; } while (u != v); } }
int n, m;
int main() { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } for (int u = 1; u <= n; u++) if (!dfn[u]) Tarjan(u, -1); sort(cutEdge.begin(), cutEdge.end()); for (auto E : cutEdge) printf("%d %d\n", E.first, E.second); return 0; }
|