您的位置:首页 > 其它

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

2016-06-08 22:00 441 查看

POJ 3177 Redundant Paths

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12598Accepted: 5330
Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. 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 <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output

Line 1: A single integer that is the number of new paths that must be built.
Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 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  :
/     :
/      :
7 + - - - -

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 is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

/*这是一个63分的代码,因为没有注意到题目中的重边问题,以后要注意有重边的图和没有重边的图的tarjan求桥的算法,是不同的*/
#include<iostream>
using namespace  std;
#include<cstdio>
#define N 5001
#define R 10010
#include<stack>
#include<queue>
#include<cstring>
queue<int>que;
bool qiao[R]={0},visit
={0},visit_edge[R<<1];
struct Edge{
int u,v,last;
}edge[R*2];
int head
,du
,f,r,father
,dfn
,low
,topt=0,t=-1;
int ans
={0};
void add_edge(int u,int v)
{
++t;
edge[t].u=u;
edge[t].v=v;
edge[t].last=head[u];
head[u]=t;
}
void input()
{
memset(head,-1,sizeof(head));
int u,v;
scanf("%d%d",&f,&r);
for(int i=1;i<=r;++i)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
r<<=1;
}
void tarjan(int u)
{
dfn[u]=low[u]=++topt;
for(int l=head[u];l!=-1;l=edge[l].last)
{
int v=edge[l].v;
if(!visit_edge[l]&&!visit_edge[l^1])
{
visit_edge[l]=true;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) qiao[l]=true;
}
else low[u]=min(low[u],dfn[v]);
}
}
}
void suo_dian()
{
for(int i=1;i<=f;++i)
{
if(!visit[i])
{
ans[++ans[0]]=i;
que.push(i);
visit[i]=true;
while(!que.empty())
{
int x=que.front();
father[x]=i;
que.pop();
for(int l=head[x];l!=-1;l=edge[l].last)
{
if(qiao[l]||visit[edge[l].v]) continue;
que.push(edge[l].v);
visit[edge[l].v]=true;
}
}

}
}
}
void re_jiantu()
{
for(int l=0;l<=r;++l)
{
if(father[edge[l].u]!=father[edge[l].v])
{
du[father[edge[l].u]]++;
du[father[edge[l].v]]++;
}
}
}
int main()
{
freopen("rpaths.in","r",stdin);
freopen("rpaths.out","w",stdout);
input();
for(int i=1;i<=f;++i)
{
if(!dfn[i])
tarjan(i);
}
suo_dian();
re_jiantu();
int sum=0;
for(int i=1;i<=ans[0];++i)
if(du[ans[i]]==2)
sum++;
printf("%d\n",(sum+1)/2);
fclose(stdin);fclose(stdout);
return 0;
}


正确代码及模板:

#define N 5011
#include<iostream>
using namespace std;
#define M 10010
#include<cstdio>
#include<cstring>
struct Gra{
int n,m,ans,head
,topt,dfn
,low
,t,cnt
;
bool visit[M<<1];
struct Edge{
int v,last;
}edge[M<<1];
void init(int f,int r)
{/*初始化不要在上面,上面只是声明,不是变量*/
ans=0,topt=0,t=-1;
n=f;m=r;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(cnt,0,sizeof(cnt));
memset(visit,false,sizeof(visit));
}
void add_edge(int x,int y)
{
++t;
edge[t].v=y;
edge[t].last=head[x];
head[x]=t;
}
void tarjan(int u)
{
dfn[u]=low[u]=++topt;
for(int l=head[u];l!=-1;l=edge[l].last)
{
if(visit[l]) continue;
visit[l]=visit[l^1]=true;/*找到无向边拆成的另一条边*/
int v=edge[l].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[v],low[u]);
}
else low[u]=min(low[u],dfn[v]);/*多次返祖*/
}
}
void start()
{
for(int i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;++i)/*处理缩点以后的图*/
for(int l=head[i];l!=-1;l=edge[l].last)
{
int v=edge[l].v;
if(low[i]!=low[v])
cnt[low[v]]++;
/*low[x]!=low[y]说明从low[y]回不到low[x],那么low[x]--low[y]是一条桥,因为tarjan中多次返祖*/
}
for(int i=1;i<=n;++i)
if(cnt[i]==1) ans++;/*统计度数是1的叶子节点的数目*/
printf("%d\n",(ans+1)>>1);
}
}G;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
G.init(n,m);
int x,y;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
G.add_edge(x,y);
G.add_edge(y,x);
}
G.start();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: