/*
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 15;
int h[N], e[N], ne[N], idx = 0;
// a指向b
void insert(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++;
}
int main()
{
memset(h, -1, sizeof h);
// n个点,m条边
int n, m;
cin >> n >> m;
while(m --)
{
int a, b;
cin >> a >> b;
insert(a, b);
}
for(int i = 1; i <= n; i++)
{
for(int j = h[i]; j != -1; j = ne[j])
{
printf("%d -> %d", i, e[j]);
puts("");
}
}
return 0;
}
邻接表存储有向图并输出(模板)
于 2023-11-01 21:12:50 首次发布