Road Construction
Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island. The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost. Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time. So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary. Input The first line of input will 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 are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions. Output One line, consisting of an integer, which gives the minimum number of roads that we need to add. Sample Input Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10 Sample Input 2 3 3 1 2 2 3 1 3 Sample Output Output for Sample Input 1 2 Output for Sample Input 2 0 Source |
提示
题意:
某旅游地要准备对于n(3<=n<=1000)座小岛之间的r(2<=r<=1000)座桥进行修理便于游客观光,在修某个桥的时候,该桥是禁止通行的。但是为了避免在修桥的时候导致游客到不了一些岛上去游玩,所以要建几个临时的桥,请你求出至少要求几座桥。
思路:
这题的本质是至少添几条边才能使得整个图为双连通图。
Tarjan缩点成一棵树,计算出这几个大点中有几个叶子节点,修桥数就为叶子数+1除于2。
这题保证没有重边,如果需要判断重边,请看:
void tarjan(int lastw,int t)
{
int pos,i;
...
for(i=h[t];i!=-1;i=w[i].next)
{
pos=w[i].v;
if(i!=(lastw^1)) //^这个符号很神奇,这样可以防止有重边,也可以判断father!=pos
{
tarjan(i,pos);
...
}
}
...
}
for(i=0;n>i;i++)
{
if(dfn[i]==-1)
{
tarjan(-1,i);
}
}
poj3177和此题一样,但有重边,如果用邻接矩阵的可以忽略。
只要记住结论就行了。。。
示例程序
Source Code
Problem: 3352 Code Length: 1864B
Memory: 420K Time: 16MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
int v,next;
}w[2000];
int h[1000],numw,dfn[1000],low[1000],vis[1000],stack[1000],id[1000],top,deep,count;
void insert(int u,int v)
{
w[numw].v=v;
w[numw].next=h[u];
h[u]=numw;
numw++;
w[numw].v=u;
w[numw].next=h[v];
h[v]=numw;
numw++;
}
void tarjan(int s,int t)
{
int pos,i;
dfn[t]=deep;
low[t]=deep;
stack[top]=t;
top++;
deep++;
vis[t]=1;
for(i=h[t];i!=-1;i=w[i].next)
{
pos=w[i].v;
if(s!=pos)
{
if(dfn[pos]==-1)
{
tarjan(t,pos);
if(low[t]>low[pos])
{
low[t]=low[pos];
}
}
else if(vis[pos]==1&&low[t]>dfn[pos])
{
low[t]=dfn[pos];
}
}
}
if(low[t]==dfn[t])
{
do
{
top--;
pos=stack[top];
vis[pos]=0;
id[pos]=count;
}while(t!=pos);
count++;
}
}
int main()
{
int i,i1,n,m,u,v,num,edge[1000];
scanf("%d %d",&n,&m);
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(vis,0,sizeof(vis));
memset(h,-1,sizeof(h));
memset(edge,0,sizeof(edge));
deep=0;
top=0;
count=0;
num=0;
for(i=1;m>=i;i++)
{
scanf("%d %d",&u,&v);
insert(u-1,v-1);
}
for(i=0;n>i;i++)
{
if(dfn[i]==-1)
{
tarjan(-1,i);
}
}
for(i=0;n>i;i++)
{
for(i1=h[i];i1!=-1;i1=w[i1].next)
{
if(id[i]!=id[w[i1].v]) //记录每个分量间边的数量
{
edge[id[w[i1].v]]++;
}
}
}
for(i=0;count>i;i++)
{
if(edge[i]==1)
{
num++; //如果边的数量为一,分量为叶子节点
}
}
printf("%d",(num+1)/2); //修桥数为叶子节点数+1除于2
return 0;
}