Redundant Paths
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Line 1: Two space-separated integers: F and R
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1: A single integer that is the number of new paths that must be built.
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
Explanation of the sample:
One visualization of the paths is:
1 2 3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
Source
题目类型:无向图求割边(Tarjan)+缩点
算法分析:这里虽然存在重边,但是可以不将重边加入到图中,然后使用low值将边重连通分量进行划分,若图中没有重边,则low值相同的顶点一定在一个边重连通分量中(因为有环)。所以此时可以双重循环枚举找邻接顶点,若两个顶点的low不同,则它们各自所在的“缩点”的度数自加1
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 |
/************************************************* 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], deg[maxn]; int n, m, id; vector <int> g[maxn]; set <PII> seta; void Init (int rt) { 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; } void dfs (int u, int par) { for (int i = 0; i < g[u].size (); i++) { int v = g[u][i]; if (!vis[v]) { vis[v] = 1; dfn[v] = low[v] = ++id; dfs (v, u); low[u] = min (low[u], low[v]); } else if (v != par) low[u] = min (low[u], dfn[v]); } } void SS () { for (int u = 1; u <= n; u++) for (int i = 0; i < g[u].size (); i++) { int v = g[u][i]; if (low[u] != low[v]) deg[low[u]]++; } } 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); if (seta.find (PII (u, v)) != seta.end ()) continue; seta.insert (PII (u, v)), seta.insert (PII (v, u)); g[u].push_back (v); g[v].push_back (u); } dfs (1, -1); SS (); int res = 0; for (int i = 1; i <= n; i++) if (deg[i] == 1) res++; printf ("%d\n", (res + 1) / 2); } return 0; } |
- « 上一篇:poj3352
- poj2762:下一篇 »