void topsort(vvi& g, vi& used, vi& ts, int u) { used[u] = 1; for(int v : g[u]) if( !used[v] ) topsort(g, used, ts, v); ts.eb(u); }
bool topsort(vvi& g, vector<char>& color, vi& ts, int u) { color[u] = 1; for(int v : g[u]) { if( color[v] == 2 ) continue; if( color[v] == 1 || !topsort(g,color,ts,v) ) return false; } ts.eb(u); color[u] = 2; return true; }