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.Simulation of the second question actually Test side double unicom component ... I'm a poor figure. Test instructions is to give a connected graph,minimum number of edges to be added so that there are two non-public edges between any two points (can have a common point)There is a conclusion to this question ... If the number of all leaf nodes after Tarjan is x, then the answer is (x+1)/2 this to draw understanding, a little trouble. (At this point you need a good seniors) in short, every time you choose the largest two points of the LCA, even without the edge, until the leaf node to finish. +1 because if 22 is left, there's one more to be connected.
#include <cstdio> #include <iostream> #define LL long longusing namespace Std;inline ll read () {ll X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int to,next;} E[1000010];int n,m,cnt=1,cnt3,tt,sum;int head[100010];int dfn[100010],low[100010],belong[100010];int zhan[100010], Top;bool inset[100010];int i[100010],o[100010];inline void ins (int u,int v) {E[++cnt].to=v;e[cnt].next=head[u];head[u ]=cnt;} inline void Insert (int u,int v) {ins (u,v); ins (v,u);} inline void dfs (int x,int fa) {zhan[++top]=x;inset[x]=1;dfn[x]=low[x]=++tt;for (int i=head[x];i;i=e[i].next) if (i!= (FA ^1)) if (!dfn[e[i].to]) {DFS (e[i].to,i); Low[x]=min (low[x],low[e[i].to]);} else if (inset[e[i].to]) low[x]=min (low[x],dfn[e[i].to]), if (Low[x]==dfn[x]) {cnt3++;int p=-1;while (p!=x) {p=zhan[ top--];belong[p]=cnt3;inset[p]=0;}}} inline void Tarjan () {for (int i=1;i<=n;i++) if (!dfn[i]) DFS (i,0);int main () {n=read (); M=read (); for (int i=1;i<=m;i++) {int x=read (), Y=read (); Insert (x, y);} Tarjan (); for (int i=1;i<=n;i++) for (int j=head[i];j;j=e[j].next) if (belong[i]!=belong[e[j].to]) {O[belong[i]]++;i [belong[e[j].to]]++;} for (int i=1;i<=cnt3;i++) if (i[i]==1) sum++;p rintf ("%d\n", (sum+1)/2);}
poj3177 redundant Paths