拓扑排序

vector<int> G[maxn];
int c[maxn];
vector<int> topo;
bool dfs(int u){
      c[u] = -1;
      for(int i = 0; i < G[u].size(); i++){
          int v = G[u][i];
        if(c[v] < 0)    return false;
        else if(!c[v]){
            if(!dfs(v)) return false;
        }
    }
    c[u] = 1;
    topo.push_back(u);
    return true;
}

bool toposort(){
      topo.clear();
      memset(c, 0, sizeof(c));
      for(int u = 1; u <= n; u++){//n为顶点个数 
          if(!c[u]){
              if(!dfs(u)) return false;
        }    
    }
      reverse(topo.begin(), topo.end());
      return true;
}