bool topsort(int n)
{
queue<int>q;
memset(h,0,sizeof h);
for(int i=1;i<=n;i++)
if(!in[i])q.push(i);
int cur=0;
while(!q.empty())
{
int x=q.front();q.pop();
cur++;
for(int e=first[x];~e;e=nex[e])
{
h[v[e]]=max(h[v[e]],h[x]+1);
if(--in[v[e]]==0)q.push(v[e]);
}
}
return cur==n;
}
void add_(int a,int b)
{
v[ecnt]=b;
nex[ecnt]=first[a];
first[a]=ecnt++;
}
拓扑排序模板
最新推荐文章于 2024-05-26 02:36:44 发布