Road Construction
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
Sample Input 2
3 3
1 2
2 3
1 3
Sample Output
Output for Sample Input 1
2
Output for Sample Input 2
0
Source
题目类型:无向图求割边(Tarjan)+缩点(并查集)
算法分析:首先可知在同一个重连通分量(边)中的点之间一定存在至少两条独立的路径,即可以不用考虑在同一个重连通分量中的点之间建路。这样的话可以将同一个连通分量缩成一个点,最后得到的图一定是一个树,且树中的边一定是割边(没有缩掉)。最后在两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先路径上所有的点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1) / 2次(leaf为树中叶子节点的个数)把所有点收缩到了一起。这里使用栈来记录每次找到的重连通分量(边),使用并查集来缩点,注意这里任意两点之间没有重边!!!
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/************************************************* Author :supermaker Created Time :2016/1/22 10:58:19 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5000 + 6; int dfn[maxn], low[maxn], vis[maxn]; int n, m, id; stack <PII> sta; vector <int> g[maxn]; set <PII> seta; int parent[maxn], deg[maxn], part[maxn]; int UnFind (int val) { if (parent[val] == val) return val; else return parent[val] = UnFind (parent[val]); } void Init (int rt) { for (int i = 0; i < maxn; i++) parent[i] = i; for (int i = 0; i < maxn; i++) g[i].clear (); seta.clear (); dfn[rt] = low[rt] = id = 1; memset (vis, 0, sizeof (vis)); memset (deg, 0, sizeof (deg)); vis[rt] = 1; while (!sta.empty ()) sta.pop (); } void SS () { for (int u = 1; u <= n; u++) if (part[u]) { for (int i = 0; i < g[u].size (); i++) { int v = g[u][i]; if (part[v]) parent[UnFind(v)] = UnFind (u); } } } void dfs (int u, int par) { for (int i = 0; i < g[u].size (); i++) { int v = g[u][i]; if (seta.find (PII (u, v)) == seta.end ()) { seta.insert (PII (u, v)); seta.insert (PII (v, u)); sta.push (PII (u, v)); if (!vis[v]) { vis[v] = 1; dfn[v] = low[v] = ++id; dfs (v, u); low[u] = min (low[u], low[v]); if (low[v] > dfn[u]) { memset (part, 0, sizeof (part)); //bool is_have = false; while (!sta.empty ()) { PII temp = sta.top (); sta.pop (); if ((temp.first == u && temp.second == v) || (temp.first == v && temp.second == u)) break; part[temp.first] = part[temp.second] = 1; //printf (" %d-%d ", temp.first, temp.second); //is_have = true; } //if (is_have) // puts (""); SS (); } } else if (v != par) low[u] = min (low[u], dfn[v]); } } } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &n, &m) != EOF) { Init (1); for (int i = 1; i <= m; i++) { int u, v; scanf ("%d%d", &u, &v); g[u].push_back (v); g[v].push_back (u); } dfs (1, -1); seta.clear (); for (int u = 1; u <= n; u++) for (int i = 0; i < g[u].size (); i++) { int v = g[u][i]; if (seta.find (PII (u, v)) != seta.end ()) continue; seta.insert (PII (u, v)), seta.insert (PII (v, u)); if (UnFind (u) != UnFind (v)) deg[UnFind(u)]++, deg[UnFind(v)]++; } int res = 0; for (int i = 1; i <= n; i++) { //cout << "i = " << i << " deg[i] = " << deg[i] << endl; if (deg[i] == 1) res++; } printf ("%d\n", (res + 1) / 2); } return 0; } |
算法分析:也可以直接使用POJ3177求解无向图(有重边)的方法来求解
链接:http://www.maksyuki.com/?p=2719
- « 上一篇:poj2942
- poj3177:下一篇 »