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

    #include<iostream>
    #include<stdlib.h>
    #include<cstring>
    #include<stdio.h>
    #define MAX 505
    using namespace std;
    
    int node[MAX];
    
    void toposort(int map[MAX][MAX],int indegree[MAX],int n)
    {
        int i,j,k,t=0;
        for(i=1; i<=n; i++) //遍历n次
        {
            for(j=1; j<=n; j++) //找出入度为0的节点
            {
                if(indegree[j]==0)
                {
                    indegree[j]--;
                    node[t++]=j;
                    for(k=1; k<=n; k++) //删除与该节点关联的边
                    {
                        if(map[j][k]==1)
                        {
                            indegree[k]--;
                        }
                    }
                    break;
                }
            }
        }
    }
    
    
    int main()
    {
        int n,m; //n:关联的边数,m:节点数
        while(scanf("%d%d",&n,&m)==2)
        {
            int i;
            int x,y;
            int map[MAX][MAX]; //邻接矩阵
            int indegree[MAX]; //入度
            memset(map,0,sizeof(map));
            memset(node,0,sizeof(node));
            memset(indegree,0,sizeof(indegree));
            for(i=0; i<m; i++)
            {
                scanf("%d %d",&x,&y);
                if(!map[x][y])
                {
                    map[x][y]=1;
                    indegree[y]++;
                }
            }
            toposort(map,indegree,n);
            for(i=0;i<n;i++)
            {
                if(i) cout<<" ";
                cout<<node[i];
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Thymeleaf+Spring使用自己的工具类
    bootstrap 响应式布局
    bootstrap 流布局
    bootstrap 布局
    bootstrap 新建网页
    quick 定时更新函数
    acm hdoj 1157
    acm hdoj 今年暑假不ac
    quick removeTileMaptile
    quick schedule 的添加和移除
  • 原文地址:https://www.cnblogs.com/calmwithdream/p/5447410.html
Copyright © 2011-2022 走看看