POJ Topic Road Construction (double connectivity)

Source: Internet
Author: User

Road Construction
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9353 Accepted: 4648

Description

It's almost summer time, and that means it ' s almost summer construction time! This year, the good people who is in charge of the roads on the tropical island paradise of "Remote" would like to R Epair and upgrade the various roads that leads between the various tourist attractions on the island.

The roads themselves is also rather interesting. Due to the strange customs of the island, the roads is arranged so then they never meet at intersections, but rather pass Over or under each of the other using bridges and tunnels. The This is the road runs between, specific tourist attractions, so that the tourists does not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road while the construction company works on a Particular road, it's unusable in either direction. This could cause a problem if it becomes impossible to travel between the tourist attractions, even if the construction Co Mpany works on only one road at any particular time.

So, the Road Department of Remote have decided to call upon your consulting services to help remedy this problem. It has been decided, that new roads would, and is built between the various attractions in such a-the-in-the-final C Onfiguration, if any one road is undergoing construction, it would still being possible to travel between any and tourist att Ractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input would consist of positive integers n and R, separated by a space, where 3≤ n ≤1000 is the number of tourist attractions on the island, and 2≤ R ≤1000 is the number of roads. The tourist attractions is conveniently labelled from 1 to N. Each of the following R lines would consist of the integers, v and W, separated by a space, Indi Cating that a road exists between the attractions labelled v and W. Note that the either direction down each road, and any pair of tourist attractions would has at the most one Roa D directly between them. Also, you were assured the current configuration, it was possible to travel between any and tourist attractions.

Output

One line, consisting of a integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 121 94 109 10Sample Input 23 31 22 31 3.

Sample Output

Output for sample input 12Output for sample input 20

Source

CCC 2007

To add how many sides become double unicom

AC Code

#include <stdio.h> #include <string.h> #define MAX (A, B) (a>b?a:b) #define MIN (A, b) (a>b?b:a) int belong [1100],low[1100],dfn[1100],ins[1100],stack[1100],head[1100],dig[1100*2];int cnt,top,time,taj,n,m;struct S{int u,v , Next;} Edge[1100*2];void init () {memset (head,-1,sizeof (head)); memset (stack,0,sizeof (stack)); memset (ins,0,sizeof (INS)); memset (dfn,-1,sizeof (DFN)); Memset (low,-1,sizeof (Low)); Memset (belong,-1,sizeof (belong)); Memset (dig,0,sizeof (Dig )); cnt=top=time=taj=0;} void Add (int u,int v) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;} void Tarjan (int u,int pre) {low[u]=dfn[u]=time++;stack[top++]=u;ins[u]=1;int flag=1;for (int i=head[u];i!=-1;i=edge[i ].next) {int v=edge[i].v;if (flag&&v==pre) {flag=0;continue;} if (dfn[v]==-1) {Tarjan (v,u); Low[u]=min (Low[u],low[v]);} ElseIf (Ins[v]) low[u]=min (Dfn[v],low[u]);} if (Low[u]==dfn[u]) {Taj++;int Now;do{now=stack[--top];ins[now]=0;belong[now]=taj;} while (now!=u);}} int main () {while (scanf ("%d%d", &n,&m)!=eof) {int i,j;inIt (); for (i=0;i<m;i++) {int a,b;scanf ("%d%d", &a,&b); add (b); add (b,a);} for (i=1;i<=n;i++) {if (dfn[i]==-1) {Tarjan (i,-1);}} for (i=0;i<cnt;i++) {int v=edge[i].v;int u=edge[i].u;if (Belong[u]!=belong[v]) {Dig[belong[u]]++;//dig[belong[v]] ++;}} for (i=1;i<=n;i++)//printf ("%d%d\n", i,belong[i]), int ans=0;for (i=1;i<=taj;i++) {//printf ("%d\n", Dig[i]); if (dig[i]==1) ans++;} printf ("%d\n", (ans+1)/2);}}


POJ Topic Road Construction (double connectivity)

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.