静态邻接表模版
来源:互联网 发布:ubuntu显示所有软件 编辑:程序博客网 时间:2024/05/16 10:05
#include<iostream>//读入,并输出#include<cstdio>#include<cstring>using namespace std;const long LMAX=2000;struct node{ int v; int next;}s[LMAX];//边int p[LMAX];//点对应下标 值为边int Index,m,n,x,y;//m点数,n边数void init(){ memset(p,-1,sizeof(p)); Index=0; for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); s[Index].next=p[x];//保存原先x所在边的序号 s[Index].v=y;//设置另一端的点 (x)----------->(y) p[x]=index p[x]=Index++;// 更新并记录点x所在边序号为当前边 (index) //上述操作将一个自该点出发而邻接的有向边链接成表 }}void print(){ for(int i=0;i<m;i++) { printf("%d:",i); for(int j=p[i];j!=-1;j=s[j].next) printf(" -> %d",s[j].v); printf("\n"); }}int main(){ while(scanf("%d%d",&m,&n)!=EOF) { init(); print(); } return 0;}