Double connected components of POJ 3352 undirected graphs

Source: Internet
Author: User

Road Construction
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9426 Accepted: 4675


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.


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.


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


CCC 2007
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <vector>6#include <map>7 using namespacestd;8 Const intmaxn=1007;9 intN,M,LOW[MAXN],SUM,DEGREE[MAXN],MP[MAXN][MAXN];Tenvector<int>G[MAXN]; One  A voidDfsintUintFA) - { -     intL=g[u].size (); thelow[u]=sum++; -      for(intI=0; i<l;i++) -     { -         intv=G[u][i]; +         if(V==FA)Continue; -         if(!Low[v]) { + DFS (v,u); A         } atlow[u]=min (low[u],low[v]); -     } - } - intTarjan () - { -      for(intI=1; i<=n;i++) in     { -          for(intj=0; J<g[i].size (); j + +) to         { +             if(Low[i]!=low[g[i][j]]) degree[low[i]]++; -         } the     } *     intres=0; $      for(intI=1; i<=n;i++)Panax Notoginseng         if(degree[i]==1) res++; -     returnRes; the } + intMain () A { the     intCnt=1, A, b; +      while(~SCANF ("%d%d",&n,&m)) { -memset (Low,0,sizeof(Low)); $memset (Degree,0,sizeof(degree)); $Memset (MP,0,sizeof(MP)); -          for(intI=0; i<=n;i++) g[i].clear (); -          for(intI=1; i<=m;i++) the         { -scanf"%d%d",&a,&b);Wuyi             if(mp[a][b]>0)Continue; themp[a][b]++;mp[b][a]++; - G[a].push_back (b); G[b].push_back (a); Wu         } -sum=1; AboutDfs1,-1); $         intans=Tarjan (); -printf"%d\n", (ans+1)/2); -     } -     return 0; A}

Double connected components of POJ 3352 undirected graphs

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: 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.