[HDOJ4738]Caocao's Bridges(双联通分量,割边,tarjan)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDOJ4738]Caocao's Bridges(双联通分量,割边,tarjan)相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738
给一张无向图,每一条边都有权值。找一条割边,使得删掉这条边双连通分量数量增加,求权值最小那条。
注意有重边,ACEveryDay里群巨给的意见是tarjan的时候记录当前点是从哪条边来的。
注意假如桥的权值是0的时候也得有一个人去炸……
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 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69 70 typedef struct Edge { 71 int u, v, w; 72 int idx, next; 73 bool cut; 74 Edge() {} 75 Edge(int uu, int vv, int ww, int ii) : u(uu), v(vv), w(ww), idx(ii) {} 76 }Edge; 77 const int maxn = 1010; 78 const int maxm = maxn*maxn; 79 int n, m; 80 int bridge; 81 Edge edge[maxm]; 82 int head[maxn]; 83 int dfn[maxn], low[maxn]; 84 int ecnt; 85 int ret; 86 87 void adde(int u, int v, int w, int i) { 88 edge[ecnt] = Edge(u, v, w, i); 89 edge[ecnt].next = head[u]; 90 edge[ecnt].cut = 0; 91 head[u] = ecnt++; 92 } 93 94 void dfs(int u, int d, int p) { 95 low[u] = dfn[u] = d; 96 for(int i = head[u]; ~i; i=edge[i].next) { 97 int v = edge[i].v; 98 int idx = edge[i].idx; 99 if(p == idx) continue; 100 if(!dfn[v]) { 101 dfs(v, d+1, idx); 102 low[u] = min(low[u], low[v]); 103 if(low[v] > dfn[u]) { 104 bridge++; 105 edge[i].cut = edge[i^1].cut = 1; 106 ret = min(ret, edge[i].w); 107 } 108 } 109 else low[u] = min(low[u], dfn[v]); 110 } 111 } 112 113 int main() { 114 // FRead(); 115 int u, v, w; 116 while(~Rint(n) && ~Rint(m) && n + m) { 117 Clr(head, -1); Cls(dfn); Cls(low); 118 ecnt = 0; bridge = 0; ret = 0x7f7f7f; 119 For(i, 1, m+1) { 120 Rint(u); Rint(v); Rint(w); 121 adde(u, v, w, i); adde(v, u, w, i); 122 } 123 int cnt = 0; 124 For(i, 1, n+1) { 125 if(!dfn[i]) { 126 cnt++; 127 dfs(i, 0, 0); 128 } 129 } 130 if(ret == 0) ret = 1; 131 if(ret == 0x7f7f7f) ret = -1; 132 if(cnt > 1) ret = 0; 133 printf("%d\n", ret); 134 } 135 RT 0; 136 }
以上是关于[HDOJ4738]Caocao's Bridges(双联通分量,割边,tarjan)的主要内容,如果未能解决你的问题,请参考以下文章
[HDU4738]Caocao's Bridges(求割边最小权值)
HDU4738 Caocao's Bridges 无向图的桥