拓扑排序
拓扑排序
判环
bool topsort()
{
int cnt = 0;
queue<int> q;
for (int i = 1; i <= n; i++)
if (!din[i]) q.push(i);
while(!q.empty())
{
int u = q.front();
q.pop();
cnt++;
for (int v : G[u])
{
din[v]--;
/*可在这里DP
dist[v] = max(dist[v], dist[u] + 1);
*/
if (!din[v])
q.push(v);
}
}
return cnt == n;//无环返回true
}
tarjan 判环与拓扑排序判环原理相同,若强连通分量个数等于点的个数则无环