[POJ3177]Redundant Paths(双连通图,割边,桥,重边)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ3177]Redundant Paths(双连通图,割边,桥,重边)相关的知识,希望对你有一定的参考价值。
题目链接:http://poj.org/problem?id=3177
和上一题一样,只是有重边。
如何解决重边的问题?
1、 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分,其中桥的一端的割点归入当前正在划分的边双连通分量。这个处理比较麻烦;
2、 在输入图G的边时,若出现重边,则不把重边放入图G,然后在划分边双连通分量时依然用Low划分。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%lld", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f, sizeof(a)) 53 #define lp p << 1 54 #define rp p << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 typedef long long LL; 58 typedef long double LD; 59 typedef unsigned long long ULL; 60 typedef pair<int, int> pii; 61 typedef pair<string, int> psi; 62 typedef map<string, int> msi; 63 typedef vector<int> vi; 64 typedef vector<LL> vl; 65 typedef vector<vl> vvl; 66 typedef vector<bool> vb; 67 68 typedef struct Edge { 69 int v; 70 bool cut; 71 Edge() {} 72 Edge(int vv) : v(vv) { cut = 0; } 73 }Edge; 74 75 const int maxn = 5500; 76 const int maxm = 555011; 77 int n, m; 78 int dig[maxn]; 79 int dfn[maxn], low[maxn], idx; 80 vector<Edge> G[maxn]; 81 bool vis[maxn]; 82 int st[maxn], top; 83 int belong[maxn], bcnt; 84 85 void tarjan(int u, int p) { 86 int v; 87 low[u] = dfn[u] = ++idx; 88 vis[u] = 1; 89 st[top++] = u; 90 Rep(i, G[u].size()) { 91 v = G[u][i].v; 92 if(v == p) continue; 93 if(!dfn[v]) { 94 tarjan(v, u); 95 low[u] = min(low[u], low[v]); 96 if(low[v] > dfn[u]) { 97 G[u][i].cut = 1; 98 Rep(j, G[v].size()) { 99 if(G[v][j].v== u) { 100 G[v][j].cut = 1; 101 break; 102 } 103 } 104 } 105 } 106 else if(vis[v]) low[u] = min(low[u], dfn[v]); 107 } 108 if(low[u] == dfn[u]) { 109 bcnt++; 110 do { 111 v = st[--top]; 112 vis[v] = 0; 113 belong[v] = bcnt; 114 } while(v != u); 115 } 116 } 117 118 int main() { 119 FRead(); 120 int u, v; 121 while(~Rint(n) && ~Rint(m)) { 122 Rep(i, n+50) G[i].cl(); 123 Cls(vis); Cls(dig); Cls(dfn); Cls(low); 124 top = 0; idx = 0; bcnt = 0; 125 Rep(i, m) { 126 Rint(u); Rint(v); 127 G[u].pb(Edge(v)); G[v].pb(Edge(u)); 128 } 129 tarjan(1, 0); 130 int ret = 0; 131 For(u, 1, n+1) { 132 printf("%d ", belong[u]); 133 Rep(i, G[u].size()) { 134 if(G[u][i].cut) { 135 dig[belong[u]]++; 136 } 137 } 138 } 139 printf("\n"); 140 For(i, 1, bcnt+1) { 141 if(dig[i] == 1) ret++; 142 } 143 printf("%d\n", (ret+1)>>1); 144 } 145 RT 0; 146 }
以上是关于[POJ3177]Redundant Paths(双连通图,割边,桥,重边)的主要内容,如果未能解决你的问题,请参考以下文章
[双连通分量] POJ 3177 Redundant Paths