zoukankan      html  css  js  c++  java
  • 拓扑排序

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int maxn=100010;
    struct edge{ int t; edge * nxt; edge(int to, edge * next){ t=to, nxt=next; } };
    edge * h[maxn];
    void add(int u, int v) { h[u]=new edge(v, h[u]); }
    int n, m, indegree[maxn], dp[maxn], seq[maxn<<1];			//seq存储其中一种拓扑序列 
     
    int topo()
    {
        queue<int>q;
        for(int i=1; i<=n; i++)	if(indegree[i]==0)	q.push(i), dp[i]=1;
    	int k=0;												//seq中存储的下标 
    	bool _unique=true;										//标记是否有唯一的排序 
        while(!q.empty())
        {
            if(q.size()>1)	_unique=false;						//有多个入度为0的点,拓扑排序不唯一 
            int u=q.front();
            q.pop();
            seq[++k]=u;
            if(k>n)	return -1;									//存在有向环,不能进行拓扑排序
            for(edge *p=h[u]; p; p=p->nxt)
            {
                indegree[p->t]--;
                dp[p->t]=max(dp[p->t], dp[u]+1);
                if(indegree[p->t]==0)	q.push(p->t);
            }
        }
        return _unique;											//1表示排序唯一,0表示有多种排序,存在seq中 
    }
    
    int main()
    {
    	scanf("%d%d", &n, &m);
    	for(int i=1, x, y; i<=m; i++) 	scanf("%d%d", &x, &y), add(x, y), indegree[y]++;
    	if(topo()>=0)
    		for(int i=1; i<=n; i++)		printf("%d
    ", dp[i]);
    	return 0;
    }
    
  • 相关阅读:
    Ubuntu(以16.04 server版为例)在VMware上安装及网络配置
    Ubuntu上面python虚拟环境的配置及mysql和redis安装
    json和pickle
    sqlalchemy第四部分
    sqlalchemy第三部分
    sqlalchemy第二部分
    线程
    文件处理
    文件流
    集合框架类
  • 原文地址:https://www.cnblogs.com/lfyzoi/p/10608683.html
Copyright © 2011-2022 走看看