链接:
http://hihocoder.com/problemset/problem/1183
代码:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <cstdlib> 10 #include <cstring> 11 #include <sstream> 12 #include <iostream> 13 #include <algorithm> 14 #include <functional> 15 using namespace std; 16 #define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,a,n) for (int i=n-1;i>=a;i--) 18 #define all(x) (x).begin(),(x).end() 19 #define pb push_back 20 #define mp make_pair 21 #define lson l,m,rt<<1 22 #define rson m+1,r,rt<<1|1 23 typedef long long ll; 24 typedef vector<int> VI; 25 typedef pair<int, int> PII; 26 const ll MOD = 1e9 + 7; 27 const int INF = 0x3f3f3f3f; 28 const int MAXN = 2e4 + 7; 29 // head 30 31 int n, m, order = 0; 32 int low[MAXN], dfn[MAXN]; 33 int par[MAXN], son[MAXN]; 34 VI G[MAXN]; 35 VI cutpoint; 36 vector<PII> cutedge; 37 38 void tarjan(int u) { 39 dfn[u] = low[u] = ++order; 40 bool flag = false; 41 rep(i, 0, G[u].size()) { 42 int v = G[u][i]; 43 if (!dfn[v]) { 44 son[u]++; 45 par[v] = u; 46 tarjan(v); 47 if (low[v] >= dfn[u]) flag = true; 48 if (low[v] > dfn[u]) cutedge.pb(mp(min(v, u), max(v, u))); 49 low[u] = min(low[u], low[v]); 50 } 51 else if (v != par[u]) low[u] = min(low[u], dfn[v]); 52 } 53 if ((par[u] == 0 && son[u] > 1) || (par[u] && flag)) cutpoint.pb(u); 54 } 55 56 int main() 57 { 58 cin >> n >> m; 59 while (m--) { 60 int u, v; 61 scanf("%d%d", &u, &v); 62 G[u].pb(v); 63 G[v].pb(u); 64 } 65 tarjan(1); 66 sort(cutedge.begin(), cutedge.end()); 67 sort(cutpoint.begin(), cutpoint.end()); 68 if (cutpoint.size() == 0) cout << "Null" << endl; 69 else { 70 cout << cutpoint[0]; 71 rep(i, 1, cutpoint.size()) printf(" %d", cutpoint[i]); 72 cout << endl; 73 } 74 rep(i, 0, cutedge.size()) 75 printf("%d %d\n", cutedge[i].first, cutedge[i].second); 76 return 0; 77 }
时间: 2024-11-07 09:10:18