[Dual-connected components] POJ 3177 Redundant Paths

Source: Internet
Author: User

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13712 Accepted: 5821

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 original question: Ask to add a few sides can make a graph into two connected components.              Problem-solving ideas: using Tarjian to shrink the picture into a tree, it is obvious that as long as the leaf node can be connected to two points, that is, if the leaf node is even, the answer is the number of leaf nodes; if odd, +1 is divisible by 2. And then find that both odd and even, all +1 are divisible by 2.
#include <stdio.h> #include <string.h>struct mp {int begin,to,next; }map[10010];int Frist[10010],num,dfn[10010],low[10010],times,bridgenum,father[10010];int degree[10010],con[10010   ],stack[10010],cnt,top,s[10010];bool count[10010];void init () {memset (father,0,sizeof (father));   memset (Count,0,sizeof (count));   memset (Con,0,sizeof (con));   memset (degree,0,sizeof (degree));   Memset (s,0,sizeof (s));   memset (stack,0,sizeof (stack));   Memset (Frist,0,sizeof (frist));   memset (dfn,0,sizeof (DFN));   memset (low,0,sizeof (Low));   memset (map,0,sizeof (map));  Num=times=bridgenum=cnt=top=0;    }void Add (int x,int y) {++num; Map[num].begin=x;map[num].to=y;    Map[num].next=frist[x];frist[x]=num;   Return   }void Tarjian (int v) {int I;bool flag=true;   Dfn[v]=low[v]=++times;   Stack[top++]=v;            for (I=frist[v];i;i=map[i].next) {if (Map[i].to==father[v]&&flag) {flag=false;       Continue;}         if (!dfn[map[i].to]) {   Father[map[i].to]=v;            Tarjian (map[i].to);  if (Low[map[i].to]<low[v]) low[v]=low[map[i].to];}   else if (Dfn[map[i].to]<low[v]) low[v]=dfn[map[i].to];     } if (Low[v]==dfn[v]) {++cnt;       do {v=stack[--top]; s[v]=cnt;} while (Dfn[v]!=low[v]);   }}int Main () {int n,m,a,b,i,u,v,k=0,ans=0,j;   Init ();   scanf ("%d%d", &n,&m);       for (i=0;i<m;++i) {scanf ("%d%d", &a,&b);   Add (A, B); add (b,a); } Tarjian (1);     for (I=1;i<=n;++i) {for (j=frist[i];j;j=map[j].next) {v=map[j].to;        if (S[i]!=s[v]) {++degree[s[i]];   ++DEGREE[S[V]]; }}} for (I=1;i<=cnt;++i) if (degree[i]==2) ++ans;   printf ("%d\n", (ans+1)/2);  return 0; }

  

[Dual-connected components] POJ 3177 Redundant Paths

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.