拓扑排序

const int M = 100005;
int a[M];
int indegree[M];
int n,m,u,v;
vector G[M];
void topo()
{
    memset(indegree,0,sizeof(indegree));
    memset(a,0,sizeof(a));
    for(int i = 1; i <= n ; i++){
        for(int j = 0 ; j <= G[i].size(); j++){
            indegree[G[i][j]]++;
        }
    }
    queue q;
    while(!q.empty())
        q.pop();
    int cnt = 1;
    for(int i = 1;i <= n ; i++){
        if(!indegree[i])
            q.push(i);
    }
    while(!q.empty()){
        int u = q.front();
        q.pop();
        a[cnt++] = u;
        for(int i = 0 ; i 

你可能感兴趣的:(常用模板)