Road Construction
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 10352 |
|
Accepted: 5140 |
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
Test instructions: Given the number of nodes and edges, determine an undirected graph and ask at least a few edges to make the graph doubly connected. (Double connectivity: Any two nodes in the graph have two or more different paths)
Idea: Use the Tarjan algorithm to narrow the two connected parts of the graph to a point and get a tree. So (the number of leaf nodes in this tree + 1)/2 is the answer.
#include"Cstdio"#include"CString"using namespacestd;Const intmaxn=1005;intv,e;BOOLMP[MAXN][MAXN];//nodes not exceeding 1000 are stored with adjacency matrices.intindex;intDFN[MAXN],LOW[MAXN];intStack[maxn],top;intCpnt[maxn],cnt;inlineintMinintAintb) { returna > B?b:a;}voidTarjan (intUintFA) {Stack[top++]=u; Dfn[u]=low[u]=++index; for(intI=1; i<=v;i++) { if(Mp[u][i]) {intv=i; if(!Dfn[v]) {Tarjan (v,u); Low[u]=min (low[u],low[v]); } Else if(V!=FA) low[u]=min (low[u],dfn[v]); } } if(low[u]==Dfn[u]) { intv; CNT++; Do{v=stack[--top]; CPNT[V]=CNT; } while(u!=v); }}intDEG[MAXN];intSeek () {intans=0; for(intI=1; i<=v;i++) for(intj=1; j<=v;j++) if(mp[i][j]&&cpnt[i]!=Cpnt[j]) {Deg[cpnt[i]]++; DEG[CPNT[J]]++; } for(intI=1; i<=cnt;i++) if(deg[i]==2) ans++; returnans;}intMain () { while(SCANF ("%d%d", &v,&e)! =EOF) {memset (MP,false,sizeof(MP)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Top=0; CNT=0; memset (CPNT,0,sizeof(CPNT)); memset (deg,0,sizeof(deg)); for(intI=0; i<e;i++) { intu,v; scanf ("%d%d",&u,&v); MP[U][V]=mp[v][u]=true; } Tarjan (1,-1); intans=Seek (); printf ("%d\n", (ans+1)/2); } return 0;}
POJ3352 (connected component indent)