拓扑排序

拓扑图 即为 有向无环图

拓扑序 满足 对于任何有子节点的节点来说,该节点的子节点在该节点的后面

拓扑排序

#include  <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 110, M = N * N;

int n;
int h[N], e[M], ne[M], idx;
int q[N];
int d[N];

void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void topdfs(){
    int hh = 0, tt = -1;
    
    for(int i = 1;i <= n;i ++)
        if(!d[i])
            q[ ++ tt] = i;
    
    while(hh <= tt){
        int u = q[hh ++];
        
        for(int i = h[u] ; ~i ; i = ne[i]){
            int j = e[i];
            
            if( (-- d[j]) == 0 ){
                q[ ++ tt] = j;
            }
        }
    }
}

int main(){
    scanf("%d",&n);
    
    memset(h, -1, sizeof h);
    
    for(int i = 1;i <= n;i ++){
        int t;
        while(scanf("%d",&t), t){
            add(i, t);
            d[t] ++;
        }
    }
    
    topdfs();
    
    for(int i = 0;i < n;i ++) printf("%d ",q[i]);
    
    return 0;
}

差分约束

这是一个差分约束的题,题目要求每个人的初始化为100,然后输入m条边
b 到 a 权值为 1 的边,求最小值,那么就是最长路

每条边的权值都为 1 直接用拓扑排序就行了

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 10010, M = 20020;

int n, m;
int h[N], e[M], ne[M], idx;
int q[N];
int d[N];
int dist[N];

int add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

bool topsort(){
    int hh = 0,tt = -1;
    for(int i = 1;i <= n;i ++)
        if(!d[i])
            q[++ tt] = i;
    
    while(hh <= tt){
        int u = q[hh ++];
        for(int i = h[u]; ~i ; i = ne[i]){
            int j = e[i];
            if(( -- d[j]) == 0)
                q[ ++ tt] = j;
        }
    }
    
    if(tt == n - 1)
        return true;
    return false;
}

int main(){
    memset(h, -1, sizeof h);
    scanf("%d%d", &n, &m);
    for(int i = 0;i < m;i ++){
        int a, b;
        scanf("%d%d", &a, &b);
        add(b, a);
        d[a] ++;
    }
    
    if(topsort()){
        int all = 0;
        for(int i = 1;i <= n;i ++)
            dist[i] = 100;
            
        for(int i = 0;i < n;i ++){
            int j = q[i];
            for(int k = h[j];~k;k = ne[k]){
                dist[e[k]] = max(dist[e[k]], dist[j] + 1);
            }
        }
        for(int i = 1;i <= n;i ++) all += dist[i];
        printf("%d\n",all);
    }
    else
        puts("Poor Xed");
    return 0;
}

bitset + topsort

该题给出一个有向无环图,然后求所有点可以到达的点的个数

在这里插入图片描述
比如对于任意一个节点u来说
u节点直接可以到达的点为1,2,3,4
然后1,2,3,4,还连着其他的边

并查集可以合并,但是无法求出一个树内的节点个位

如果对于每一个节点来说, 都进行一边搜索的话,复杂度太高了

N M 都是30000,N*M的话就是9e8的数量级,基本要做到线性的遍历完所有的边和点就完成了操作

就得想一种可以压缩的方法,把u节点连接的点是u本身并上1并上2并上3并上4,而且还得是线性的

那就是用二进制去存储,二进制的每一位的01值为对应位置的节点是否被该节点连通,

那么u = u | 1 | 2 | 3 | 4 了,这几个值并在一起就行了

bitset有点像二维数组,

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;

const int N = 30030, M = 30030;

int n, m;
int h[N], ne[M], e[M], idx;
int d[N], q[N];
bitset <N> f[N];

void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void topsort(){
    int hh = 0, tt = -1;
    for(int i = 1;i <= n;i ++)
        if(!d[i])
            q[++ tt] = i;
    
    while(hh <= tt){
        int u = q[hh ++];
        for(int i = h[u]; ~i; i = ne[i]){
            int j = e[i];
            if(-- d[j] == 0)
                q[ ++ tt] = j;
        }
    }
    return ;
}

int main(){
    memset(h, -1, sizeof h);
    scanf("%d%d", &n, &m);
    while(m --){
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
        d[b] ++;
    }
    
    topsort();

    for(int i = n - 1 ; i >= 0;i --){
        int j = q[i];
        f[j][j] = 1;
        for(int k = h[j];~k;k = ne[k]){
            f[j] |= f[e[k]];
        }
    }
    for(int i = 1;i <=n ;i ++)
        printf("%d\n",f[i].count());
}

分层求拓扑序
代码

字典序最小拓扑序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值