bool Topo() { int sum = 0; while(1) { queue <int> Q; for(int i = 1; i <= n; i++) if(!in[i]) Q.push(i); sum += Q.size(); if(sum == n) return true; if(!Q.size()) return false; while(!Q.empty()) { int u = Q.front(); Q.pop(); in[u]--; for(int v = first[u]; v != -1; v = e[v].next) { in[e[v].v]--; } } } }
拓扑排序模版
时间: 2024-09-28 16:22:40