首页 > 其他 > 详细

洛谷 P3916 【图的遍历】反向加边+dfs

时间:2018-11-02 22:48:50      阅读:162      评论:0      收藏:0      [点我收藏+]

前言:

对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值。因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而dfs一旦你回溯完环上某个点就不会在更新这个点的答案了,做不到同时更新。

所以这类题dfs有后效性,于是我们打算消除这个后效性:我们从编号最大的点开始枚举,这样dfs能到的点从一开始被遍历是就是最优解了,根本不需要更新,完全没有后效性。但因为我们是从编号最大的点开始向后遍历的,所以需要反向加边(其实就是从子节点遍历他父亲而已)

于是就做完了:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>

#define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int

using namespace std;

struct su{
    int to,next;
}a[100001];

bool vis[100001];
int big[100001];//就是答案
int tou[100001];
int n,m,b,c,now;

inline int qr(){//快读
    char ch;
    while((ch=getchar())<‘0‘||ch>‘9‘);
    int res=ch^48;
    while((ch=getchar())>=‘0‘&&ch<=‘9‘)
        res=(res<<1)+(res<<3)+(ch^48);
    return res;
}

inline void dfs(int i){
    vis[i]=1; big[i]=now;//直接填上最优解
    for(rg j=tou[i];j;j=a[j].next)
        if(!vis[a[j].to])dfs(a[j].to);
}

int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    n=qr(),m=qr();
    for(rg i=1;i<=m;++i){
        a[i].to=qr();//反向加边
        a[i].next=tou[b=qr()];
        tou[b]=i;
    }
    for(rg i=n;i>=1;--i)//从编号最大的 n 开始向上遍历
        if(!vis[i])now=i,dfs(i);
    for(rg i=1;i<=n;++i)
        printf("%d ",big[i]);//输出
    return 0;
}

洛谷 P3916 【图的遍历】反向加边+dfs

原文:https://www.cnblogs.com/812-xiao-wen/p/9898558.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!