题解:【ABC318G】 Typical Path Problem

题目链接

无脑圆方树。建广义圆方树,对于路径 \(u \to v\) 上的圆点为必须经过的割点,经过的方点连出去的任意一个点 \(z\),记路径上和方点相连的两个圆点为 \(x,y\),原图必定存在一条简单路径 \(x \to z \to y\),证明考虑网络流。根据这个性质将 \(b\) 在的方点全部赋值为 \(1\),然后在圆方树上暴跳检查 \(a \to c\) 的路径上有无权为 \(1\) 的点即可。于是做到 \(\mathcal O(n + m)\),复杂度是 tarjan 和树上 dfs。

有无佬教教有向图怎么做啊?昨晚读错题罚坐半小时,感觉 DAG 都不会做。

#include<bits/stdc++.h>
#define ld long double
#define ui unsigned int
#define ull unsigned long long
#define int long long
#define eb emplace_back
#define pb pop_back
#define ins insert
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define power(x) ((x)*(x))
using namespace std;

namespace FastIO
{
    template<typename T=int> inline T read()
    {
        T s=0,w=1; char c=getchar();
        while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
        while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
        return s*w;
    }
    template<typename T> inline void read(T &s)
    {
        s=0; int w=1; char c=getchar();
        while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
        while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
        s=s*w;
    }
    template<typename T,typename... Args> inline void read(T &x,Args &...args)
    {
        read(x),read(args...);
    }
    template<typename T> inline void write(T x,char ch)
    {
        if(x<0) x=-x,putchar('-');
        static char stk[25]; int top=0;
        do {stk[top++]=x%10+'0',x/=10;} while(x);
        while(top) putchar(stk[--top]);
        if(ch!='~') putchar(ch);
        return;
    }
}
using namespace FastIO;

namespace MTool
{   
    #define TA template<typename T,typename... Args>
    #define TT template<typename T>
    static const int Mod=998244353;
    TT inline void Swp(T &a,T &b) {T t=a;a=b;b=t;}
    TT inline void cmax(T &a,T b) {a=max(a,b);}
    TT inline void cmin(T &a,T b) {a=min(a,b);}
    TA inline void cmax(T &a,T b,Args... args) {a=max({a,b,args...});}
    TA inline void cmin(T &a,T b,Args... args) {a=min({a,b,args...});}
    TT inline void Madd(T &a,T b) {a=a+b>=Mod?a+b-Mod:a+b;}
    TT inline void Mdel(T &a,T b) {a=a-b<0?a-b+Mod:a-b;}
    TT inline void Mmul(T &a,T b) {a=a*b%Mod;}
    TT inline void Mmod(T &a) {a=(a%Mod+Mod)%Mod;}
    TT inline T Cadd(T a,T b) {return a+b>=Mod?a+b-Mod:a+b;}
    TT inline T Cdel(T a,T b) {return a-b<0?a-b+Mod:a-b;}
    TT inline T Cmul(T a,T b) {return a*b%Mod;}
    TT inline T Cmod(T a) {return (a%Mod+Mod)%Mod;}
    TA inline void Madd(T &a,T b,Args... args) {Madd(a,Cadd(b,args...));}
    TA inline void Mdel(T &a,T b,Args... args) {Mdel(a,Cadd(b,args...));}
    TA inline void Mmul(T &a,T b,Args... args) {Mmul(a,Cmul(b,args...));}
    TA inline T Cadd(T a,T b,Args... args) {return Cadd(Cadd(a,b),args...);}
    TA inline T Cdel(T a,T b,Args... args) {return Cdel(Cdel(a,b),args...);}
    TA inline T Cmul(T a,T b,Args... args) {return Cmul(Cmul(a,b),args...);}
    TT inline T qpow(T a,T b) {int res=1; while(b) {if(b&1) Mmul(res,a); Mmul(a,a); b>>=1;} return res;}
    TT inline T qmul(T a,T b) {int res=0; while(b) {if(b&1) Madd(res,a); Madd(a,a); b>>=1;} return res;}
    TT inline T spow(T a,T b) {int res=1; while(b) {if(b&1) res=qmul(res,a); a=qmul(a,a); b>>=1;} return res;}
    TT inline void exgcd(T A,T B,T &X,T &Y) {if(!B) return X=1,Y=0,void(); exgcd(B,A%B,Y,X),Y-=X*(A/B);}
    TT inline T Ginv(T x) {T A=0,B=0; exgcd(x,Mod,A,B); return Cmod(A);}
    #undef TT
    #undef TA
}
using namespace MTool;

inline void file()
{
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
    return;
}

bool Mbe;

namespace LgxTpre
{
    static const int MAX=200010;
    static const int inf=2147483647;
    static const int INF=4557430888798830399;
    
    int n,m,a,b,c;
    int x,y,ans;
    int dep[MAX<<1],fa[MAX<<1];
    
    namespace RST
	{
	    vector<int> G[MAX],T[MAX<<1];
	    int dfn[MAX],low[MAX],tot;
	    int stk[MAX],val[MAX<<1],top,num;
	    void tarjan(int now)
	    {
	        dfn[now]=low[now]=++tot;
	        stk[++top]=now;
	        for(auto to:G[now])
	            if(!dfn[to])
	            {
	                tarjan(to),cmin(low[now],low[to]);
	                if(low[to]==dfn[now])
	                {
	                    ++num;
	                    do T[num].eb(stk[top]),T[stk[top]].eb(num),--top,val[num]|=(stk[top]==b); while(stk[top+1]!=to);
	                    T[num].eb(now),T[now].eb(num),val[num]|=(now==b);
	                }
	            }
	            else cmin(low[now],dfn[to]);
	    }
	    inline void build() {for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);}
	}
	using namespace RST;

    inline void mian()
    {	
		read(n,m,a,b,c),num=n;
    	for(int i=1;i<=m;++i) read(x,y),G[x].eb(y),G[y].eb(x);
    	RST::build();
    	
    	auto dfs=[&](auto dfs,int now,int father)->void
    	{
    		dep[now]=dep[father]+1,fa[now]=father;
			for(auto to:T[now]) if(to!=father) dfs(dfs,to,now); 
		};
		dfs(dfs,1,0);
    	
    	while(a!=c) dep[a]>dep[c]?(a=fa[a],ans|=val[a]):(c=fa[c],ans|=val[c]);
		puts(ans?"Yes":"No");
	}
}

bool Med;

signed main()
{
//  file();
    fprintf(stderr,"%.3lf MB\n",abs(&Med-&Mbe)/1048576.0);
    int Tbe=clock();
    LgxTpre::mian();
    int Ted=clock();
    cerr<<1e3*(Ted-Tbe)/CLOCKS_PER_SEC<<" ms\n";
    return (0-0);
}
posted @ 2023-09-03 15:13  LgxTpre  阅读(16)  评论(0编辑  收藏  举报