链式前向星的存储模板
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
const int N = 1000005, M = 2e6 + 5;
int head[N], cnt;//※
struct ed
{
int from, to, next;
int w;
}edge[N];//※
void init()
{
for (int i = 0; i < N; i++)head[i] = -1;
for (int i = 0; i < N; i++)edge[i].next = -1;
cnt = 0;
}
//highlight:::
void addedge(int u, int v, int w)
{
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];//※
head[u] = cnt ++ ;//※
}
int main()
{
init();
int n, m; cin >> n >> m;//n个点,m条边
for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; addedge(u, v, w); }
//遍历节点2的所有邻居
for (int i = head[2]; i != -1; i = edge[i].next)
{
cout << edge[i].to;
}
return 0;
}
标签:图论,int,cnt,板子,next,edge,include,head
From: https://www.cnblogs.com/zzzsacmblog/p/18106911