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 7 71 22 33 42 54 55 65 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 : / : / 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.Source Usaco 2006 January Gold |
Test instructions: is to join the least side to make it into a double link diagram
A bridge with the link to become a double-link map, the double-connected sub-map to deal with, so that it becomes a tree (no ring)
Find the Tree leaf node (in degrees 1) plus the number of edges (leaf+1)/2;
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack> #include <bitset>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, n ) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long long Ll;typedef pair& lt;int,int>pil;const int maxn=5000+10;const int maxm=10000+10;struct node{int to,next; BOOL col;//for bridge}e[maxm];int head[maxn],cnt;int Dfn[maxn],low[maxn];int s[maxn],instack[maxn];int idex,top,bridge;int Belong[maxn],in[maxn];int n,m;void init () {cnt=top=idex=bridge=0; CLEAR (head,-1); CLEAR (dfn,0); CLEAR (low,0); CLEAR (instack,0); CLEAR (belong,0); CLEAR (in,0);} void Addedge (int u,int v) {E[cnt].to=v;e[cnt].next=head[u]; e[cnt].col=false;head[u]=cnt++;} void Tarjan (int u,int pre) {int V; Low[u]=dfn[u]=++idex; S[top++]=u; Instack[u]=1; for (int i=head[u];i!=-1;i=e[i].next) {v=e[i].to; if (v==pre) continue; if (! Dfn[v]) {Tarjan (v,u); if (Low[u]>low[v]) low[u]=low[v]; if (Low[v]>dfn[u])//bridge {bridge++; E[i].col=true; E[i^1].col=true; }} else if (Instack[v]&&low[u]>dfn[v]) low[u]=dfn[v]; } if (Low[u]==dfn[u]) {cnt++; do{V=s[--top]; instack[v]=0; belong[v]=cnt; }while (V!=u); }}void work () {REPF (i,1,n) if (! Dfn[i]) Tarjan (i,i); for (int i=1;i<=n;i++) {for (int j=head[i];j!=-1;j=e[j].next) if (e[j].col) in[belong[i]]++; } int ans=0; REPF (i,1,cnt) if (in[i]==1) ans++; printf ("%d\n", (ans+1)/2);} int main () {int u,v; while (~SCANF ("%d%d", &n,&m)) {init (); REPF (i,1,m) {scanf ("%d%d", &u,&v); Addedge (U,V); Addedge (V,u); } work (); } return 0;}
POJ 3177 redundant Paths (side double unicom)