邻接表使用vector实现
void read()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
edge[x].push_back(y);
in[y]++;
}
}
void topo_sort()
{
for (int i = 1; i <= n;i++)
if (!in[i]) que.push(i);
while (!que.empty())
{
int v = que.front();
que.pop();
ans.push_back(v);
for (int i = 0; i < edge[v].size(); i++)
{
if (--in[edge[v][i]] == 0)
que.push(edge[v][i]);
}
}
}
使用数组实现邻接表
void add_edge(int from, int to)
{
ver[++tot] = to;
Next[tot] = head[from];
head[from] = tot;
in[to]++;
}