[UVA796]Critical Links(割边, 桥)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[UVA796]Critical Links(割边, 桥)相关的知识,希望对你有一定的参考价值。
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737
求桥的数量,也就是割边的数量。输入有点小坑,左右括号外必须得有个空格才行,起初以为是转义的问题。
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 36 using namespace std; 37 38 #define fr first 39 #define sc second 40 #define cl clear 41 #define BUG puts("here!!!") 42 #define W(a) while(a--) 43 #define pb(a) push_back(a) 44 #define Rint(a) scanf("%d", &a) 45 #define Rll(a) scanf("%lld", &a) 46 #define Rs(a) scanf("%s", a) 47 #define Cin(a) cin >> a 48 #define FRead() freopen("in", "r", stdin) 49 #define FWrite() freopen("out", "w", stdout) 50 #define Rep(i, len) for(int i = 0; i < (len); i++) 51 #define For(i, a, len) for(int i = (a); i < (len); i++) 52 #define Cls(a) memset((a), 0, sizeof(a)) 53 #define Clr(a, x) memset((a), (x), sizeof(a)) 54 #define Full(a) memset((a), 0x7f7f, sizeof(a)) 55 #define lp p << 1 56 #define rp p << 1 | 1 57 #define pi 3.14159265359 58 #define RT return 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 const int maxn = 100010; 71 typedef struct Bridge { 72 int u, v; 73 Bridge() {} 74 Bridge(int uu, int vv) : u(uu), v(vv) { if(u > v) swap(u, v); } 75 }Bridge; 76 77 int n; 78 int dfn[maxn], low[maxn]; 79 vi G[maxn]; 80 vector<Bridge> b; 81 82 void dfs(int u, int d, int p) { 83 low[u] = dfn[u] = d; 84 Rep(i, G[u].size()) { 85 int v = G[u][i]; 86 if(!dfn[v]) { 87 dfs(v, d+1, u); 88 low[u] = min(low[u], low[v]); 89 if(low[v] > dfn[u]) b.push_back(Bridge(u, v)); 90 } 91 else if(p != v) low[u] = min(low[u], dfn[v]); 92 } 93 } 94 95 bool cmp(Bridge x, Bridge y) { 96 if(x.u == y.u) return x.v < y.v; 97 return x.u < y.u; 98 } 99 100 int main() { 101 // FRead(); 102 int u, v, p; 103 while(~Rint(n)) { 104 if(n == 0) { 105 printf("0 critical links\n\n"); 106 continue; 107 } 108 Cls(dfn); Cls(low); b.cl(); 109 Rep(i, n+5) G[i].cl(); 110 Rep(i, n) { 111 Rint(u); 112 scanf(" (%d) ", &p); 113 W(p) { 114 Rint(v); 115 G[u].push_back(v); 116 G[v].push_back(u); 117 } 118 } 119 Rep(i, n) if(!dfn[i]) dfs(i, 0, i); 120 sort(b.begin(), b.end(), cmp); 121 printf("%d critical links\n", b.size()); 122 Rep(i, b.size()) { 123 printf("%d - %d\n", b[i].u, b[i].v); 124 } 125 printf("\n"); 126 } 127 RT 0; 128 }
以上是关于[UVA796]Critical Links(割边, 桥)的主要内容,如果未能解决你的问题,请参考以下文章
UVA 796 Critical Links —— (求割边(桥))
UVA796 - Critical Links(Tarjan求桥)
UVA - 796 Critical Links (tarjan,无向图桥)