浅谈链式前向星

写在最前:笔者其实对链式前向星的理解并不是很深刻,老是忘记怎么写,每次都要看模板。而最近的一次,模板出现了问题。于是,记录下正确的写法。
所用如下:
数组 h e a d [ i ] head[i] head[i] 大小为点的数量
h e a d [ i ] head[i] head[i]:最后一条起点为i的边的序号
变量 e _ c n t e\_cnt e_cnt 最终大小(加边操作全部结束后)为边的数量
e _ c n t e\_cnt e_cnt:维护每条边的序号
结构体 e [ i ] { t o , n e x t , w e i g h t } e[i] \left\{to,next,weight \right\} e[i]{to,next,weight} 大小为边的数量
e [ i ] . t o e[i].to e[i].to:编号为i的边的终点  e [ i ] . n e x t e[i].next e[i].next:起点与编号为i的边相同的上一条边  e [ i ] . w e i g h t e[i].weight e[i].weight:编号为i的边的权值
c o d e code code 存储:

void add_edge(int u,int v,int w{
	e[++e_cnt].next=head[u];
	e[e_cnt].to=v;
	e[e_cnt].weight=w;
	head[u]=e_cnt;
	return;
}

c o d e code code 遍历:

for(int i=head[s];i;i=e[i].next{
	int to=e[i].to;
	......
	//s就是初始的点。
	//比如某个dfs程序为void dfs(int now,int from),则s就是now
}

附 实际使用中的一份代码:

inline void get_sum(int now,int fro){
	sum[now]=tim[now];
	for(reg int i=he[now];i;i=e[i].nxt){
		int to=e[i].to;
		if(to==fro)continue;
		get_sum(to,now);
		sum[now]+=sum[to];
	}
	return;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值