zoukankan      html  css  js  c++  java
  • 洛谷 P3916 【图的遍历】反向加边+dfs

    前言:

    对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值。因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而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;
    }
    
    
    ✐☎博主撰文不易,转载还请注明出处;若对本文有疑,请私信或在下方讨论中提出。O(∩_∩)O谢谢!☏

    ☃〔尽管小伙伴们肯定有千百种方式针对,但博主还是极其非常十分不要脸的把反对键吃掉辣!〕☃

    ✿『$At$ $last$:非常一(hu)本(shuo)正(ba)经(dao)的:博主很笨,请不要欺负他』✿✍

  • 相关阅读:
    LeetCode "Palindrome Partition II"
    LeetCode "Longest Substring Without Repeating Characters"
    LeetCode "Wildcard Matching"
    LeetCode "Best Time to Buy and Sell Stock II"
    LeetCodeEPI "Best Time to Buy and Sell Stock"
    LeetCode "Substring with Concatenation of All Words"
    LeetCode "Word Break II"
    LeetCode "Word Break"
    Some thoughts..
    LeetCode "Longest Valid Parentheses"
  • 原文地址:https://www.cnblogs.com/812-xiao-wen/p/9898558.html
Copyright © 2011-2022 走看看