您的位置:首页 > 其它

22.4-2简单路径条数

2016-03-31 15:51 162 查看
利用有向无环图的性质,不存在后向边,只存在树边前向边和横向边

给节点增加一个属性,即节点到目标节点的简单路径条数

考虑节点u

v是白色的,则v.count还没有计算,计算v.count,再u.count+=v.count

v是黑色的,则将u.count+=v.count,避免重复计算,因此具有动态规划性质

v不可能是灰色的

算法的复杂度和dfs相同为o(v+e)

#include<stdio.h>
#include<stdlib.h>
#define white 1
#define gray 2
#define black 3
typedef struct edge{
int i;
struct edge *next;
}edge;
typedef struct node{
int d,f,color,count;
edge *next;
}node;
typedef struct gve{
int v,e;
node *g;
}gve;
int insert(node *pnode,int i)
{
edge *new;
new=(edge *)malloc(sizeof(edge));
new->i=i;
new->next=pnode->next;
pnode->next=new;
return 0;
}
int dfsvisit(gve G,node *pnode,int *time)
{
edge *tmp=pnode->next;
(*time)++;
pnode->d=*time;
pnode->color=gray;
while (tmp!=NULL)
{
if (G.g[tmp->i].color==white)
dfsvisit(G,&G.g[tmp->i],time);
pnode->count+=G.g[tmp->i].count;
tmp=tmp->next;
}
pnode->color=black;
(*time)++;
pnode->f=*time;
return 0;
}
int main(void)
{
int i;
gve G;
scanf("%d%d",&G.v,&G.e);
G.g=(node *)malloc(sizeof(node)*(G.v+1));
for (i=1;i<=G.v;i++)
{
G.g[i].count=0;
G.g[i].next=NULL;
G.g[i].color=white;
}
int s,d;
for (i=1;i<=G.e;i++)
{
scanf("%d%d",&s,&d);
insert(&G.g[s],d);
}
scanf("%d%d",&s,&d);
G.g[d].count=1;
int time=0;
dfsvisit(G,&G.g[s],&time);
printf("%d\n",G.g[s].count);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: