Redundant Paths
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10798 |
|
Accepted: 4626 |
Description
In order to get from one of the F (1 <= f <= 5,000) grazing fields (which is numbered 1..F) to another field, Bessi E and the rest of the herd is forced to cross near the Tree of rotten Apples. The cows is now tired of often being forced to take a particular path and want to build some new paths so that they would Always has a choice of at least, separate routes between any pair of fields. They currently has at least one route between each pair of fields and want to has at least. 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 <=) paths This each connect exactly both different field s, determine the minimum number of new paths (each of the which connects exactly.) that must is built so that there is at least, separate routes between any pair of fields. Routes is considered separate if they use none of the same paths, even if they visit the same intermediate field along th e-To.
There might already be more than one paths between the same pair of fields, and your may also build a new path that connect s the same fields as some and other path.
Input
Line 1:two space-separated integers:f and R
Lines 2..r+1:each line contains, space-separated integers which is the fields at the endpoints of some path.
Output
Line 1: A single integer which is the number of the new paths this must be built.
Sample Input
Sample Output
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 : / : /
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 are, in fact, connected by and routes.
It's possible that adding some other path would also solve the problem (like one from 6 to 7). Adding paths, however, is the minimum.
The following parsing comes from: Goddess of the blog
The Bin God blog has a good summary: Bin God's Blog
Approximate test instructions:
In order to protect the grazing environment and avoid excessive biting of the turf in the same place, ranchers decided to use the continuous migration of livestock to feed the way to protect the pasture. However, livestock will also gnaw on the forage on the road during the migration, so if the same road is used for each migration, the road will be damaged by biting too much.
The ranchers now have an F farm, which is known to have at least one path connected (not necessarily directly connected), but from some farms there is at least one way to go. In order to protect the pasture on the road, the farmer hopes to build several more roads, so that at least 2 migration routes will be made each time the livestock is migrated, avoiding the repeated steps to the migration path. Given the current R Road, ask the farmer to build at least a few new roads to meet the requirements.
Problem Solving Ideas:
"There are at least 2 migration pathways for each migration of livestock, avoiding the repetitive path of migration. "That is, when the F farm is regarded as a point, the road as a side structure of a graph G, there is no bridge.
Then you can build the model:
Given a connected undirected graph G, it is necessary to add at least a few edges to make it a double-connected graph.
When Figure G has a bridge (cut edge), it must not be double-connected. The two endpoints of the bridge must belong to the two "side two connected components" of figure g respectively, and once the bridge is removed, the two "side-connected components" must be disconnected, and the figure G is not connected. However, if you add an edge between the two "side two-connected components", the bridge is no longer a bridge, and the two "edge-to-double-connected components" are doubly connected.
So what if figure G has multiple "edge-to-double-connected" components? At least how many edges should be added to make any two "edge two connected components" are double connected (that is, figure G is a double-connected)
1, the first to find out all the "side of the double-connected components of Figure G."
2, each "side of the two connected components" as a point (that is, "shrinking point")
3 . The problem is again converted to "at least how many tree edges are added to the indent tree, making the tree a double connected graph".
First you know an equation:
To make any tree, after adding several edges, becomes a double connected graph, then
At least the increased number of edges = (the total amount of nodes in this tree is 1 + 1)/2.
#include <cstdio> #include <cstring> #include <algorithm> #include <stack> #define MAXN 5010# Define MAXM 20010using namespace Std;int N, m;struct node {int u, V, next;}; Node edge[maxm];//to form a tree, the degree of each point int du[maxn];int head[maxn], Cnt;int LOW[MAXN], the value of the Dfn[maxn];//belong array is 1 ~ Ebc_block int STACK[MAXN], Belong[maxn];int ebc_block;//side double connected block number int dfs_clock;int top;//analog stack pointer bool instack[maxn];void init () {cnt = 0; Memset (Head,-1, sizeof (head));} void Addedge (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;} void Getmap () {while (m--) {int A, B; scanf ("%d%d", &a, &b); Addedge (A, b); Addedge (b, a); }}void Tarjan (int u, int pre) {int V; Low[u] = dfn[u] = ++dfs_clock; stack[top++] = u; Instack[u] = true; int has = 1; for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; if (with && v = = Pre) {//de-heavy edges have = 0; Continue;} if (!dfn[v]) { Tarjan (V, u); Low[u] = min (Low[u], low[v]); } else if (Instack[v]) low[u] = min (Low[u], dfn[v]); } if (low[u] = = Dfn[u]) {ebc_block++; do{v = stack[--top]; INSTACK[V] = false; BELONG[V] = Ebc_block; } while (V! = u); }}void Suodian () {memset (Du, 0, sizeof (DU)); for (int i = 0; i < cnt; i + = 2) {int u = belong[edge[i].u]; int v = belong[edge[i].v]; if (U = v) du[u]++, du[v]++; }}void find () {memset (DFN, 0, sizeof (DFN)); memset (Low, 0, sizeof (low)); Memset (Instack, False, sizeof (Instack)); memset (Belong, 0, sizeof (Belong)); Dfs_clock = 0; Ebc_block = 0; top = 0; Tarjan (1,-1);//Connect graph}void Solve () {int ans = 0; if (Ebc_block = = 1) {printf ("0\n"); return; } for (int i = 1; I <= ebc_block; ++i) if (du[i] = = 1) ans++; printf ("%d\n", (ans + 1)/2);} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {init (); Getmap (); Find (); Suodian (); Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3177--redundant Paths "undirected graph Add least edge becomes edge double connected graph && Tarjan for EBC && indent construct indent tree"