其实这个东西,如果已经理解链式前向星存边了的话应该都想的出来。如果还不理解链式前向星还是先学吧。
struct edge{
int fr,to,top,bot;//fr为起点, to为终点, top为边集栈中该边上方一边, bot为边集栈中该边下方一边
}e[2000005];
int head[2005],ecnt=0;
inline void add_edge(int u, int v){
e[++ecnt].fr = u; //编辑起点
e[ecnt].to = v; //编辑终点
e[ecnt].top = 0; //因为新加入的边为栈顶,所以上方不存在边,设为0
e[head[u]].top = ecnt; //原栈顶的上方设为该边
e[ecnt].bot = head[u]; //该边的下方设为原栈顶
head[u] = ecnt; //新栈顶为该边
return ;
}
inline void delete_edge(int now){//now为要删除的边的编号
int up=e[now].top, down=e[now].bot;
if (up == 0) head[e[now].fr] = down;//栈顶改变
else e[up].bot = down; //上方边的下方改变
if (down>0) e[down].top = up; //下方边的上方改变
return ;
}