链式前向星
大佬原博客讲的很清楚了,自己记个板子写法以及理解的注释
大佬博客:https://blog.csdn.net/ACdreamers/article/details/16902023
int cnt;
int head[N];
struct node{
int to,next,w;
//to表示第i条边的终点,next表示与第i条边同起点的下条边的存储位置,w为边权值
}edge[N<<1];
void add(int u,int v){ //边是u到v
edge[cnt].to=v; //第i条边的终点
edge[cnt].next=head[u]; //以u为起点的边的上一条的序号
head[u]=cnt++; //以u为起点的最后一条边的序号
}
int main(int argc, char * argv[]) {
ios::sync_with_stdio(false);
int n;
cin>>n;
mef(head);
int a,b;
for(int i=0;i<n;i++){
cin>>a>>b;
add(a,b);
add(b,a);
}
return 0;
}