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

    太简单了不写“笔记”了

    图的拓扑排序

     1 //注:大部分拓扑排序的题都需要SPJ,因为不同的数据结构的原因,拓扑排序有很多种输出。
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<vector>
     6 #include<cmath>
     7 #include<queue>
     8 using namespace std;
     9 struct edge{
    10     int v,next;
    11 }a[1000001];
    12 int n,s,tot=0,head[100001],num[100001];
    13 void add(int u,int v){
    14     num[v]++;
    15     a[++tot].v=v;
    16     a[tot].next=head[u];
    17     head[u]=tot;
    18 }
    19 void dfs(int u){
    20     int v;
    21     priority_queue<int,vector<int>,greater<int> >q;//这里用queue、stack都行
    22     for(int i=1;i<=u;i++){
    23         if(num[i]==0){
    24             num[i]--;
    25             q.push(i);
    26         }
    27     }
    28     while(!q.empty()){
    29         v=q.top();
    30         q.pop();
    31         printf("%d ",v);
    32         for(int tmp=head[v];tmp!=-1;tmp=a[tmp].next){
    33             num[a[tmp].v]--;
    34             if(num[a[tmp].v]==0)q.push(a[tmp].v);
    35         }
    36     }
    37 }
    38 int main(){
    39     memset(head,255,sizeof(head));
    40     memset(num,0,sizeof(num));
    41     scanf("%d",&n);
    42     for(int i=1;i<=n;i++){
    43         scanf("%d",&s);
    44         while(s){
    45             add(i,s);
    46             scanf("%d",&s);
    47         }
    48     }
    49     dfs(n);
    50     return 0;
    51 }
  • 相关阅读:
    LVM
    Linux 压缩归档
    <基础> PHP 字符串操作
    <基础> PHP 数组操作
    PHP 文件操作类(转载)
    Linux 磁盘管理(分区、创建文件系统、挂载)
    文件系统(File System)
    Linux 硬链接、软链接
    Django基础一
    数据库约束
  • 原文地址:https://www.cnblogs.com/dcdcbigbig/p/8945253.html
Copyright © 2011-2022 走看看