请看完后点个赞,谢谢
1 邻接表存图
//邻接表存图模版 22-04-11
#include <bits/stdc++.h>
using namespace std;
const int N=100;
int h[N],vtx[2*N],nxt[2*N],idx=0;
void add_edge(int a,int b)
{
nxt[idx]=h[a];h[a]=idx;vtx[idx]=b;idx++;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,a,x,y;
cin>>n>>m;
//初始化-1
memset(h,-1,sizeof(h));
for (int i=0;i<m;i++)
{
cin>>a>>x>>y;
add_edge(x,y);
if (a==1) add_edge(y,x);
}
int p;
for (int i=0;i<=n;i++)
{
p=h[i];//链表指针
cout<<i<<":";
//循环遍历顶点下面所有链接的点
while (p!=-1)
{
cout<<" "<<vtx[p];p=nxt[p];
}
cout<<endl;
}
return 0;
}