CF555E
这个题看着就和图的连通性有关,排除点双,所以就用边双了。所以先进行一次边双缩点。首先不在一个连通块里的显然是无解的,然后再分
#include <bits/stdc++.h>
//#define int long long
#define ull unsigned long long
#pragma GCC optimeze(3)
#pragma GCC optimeze(2)
#define PII pair<int, int>
#define pb push_back
#define fi first
#define se second
#define lowbit(x) (x & (-x))
#define inv(x) (qpow(x,mod-2))
#define blong(i) ((i+K-1)/K)
using namespace std;
const int N=2e5+10;
const int M=3e2+5;
const int mod=9901;
double eps=1e-6;
inline int read(){
char ch=getchar();bool f=0;int x=0;
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;
for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
if(f==1)x=-x;return x;
}
int n,m,k,bj1[N],bj2[N],u[N],v[N],dep[N],fa[N][20],bj[N],dfn[N],low[N],dfc,edc[N],cnt;
vector<int>G[N],G2[N];
void dfs(int t){
bj[t]=dfc;
for(auto to:G2[t]){
if(to==fa[t][0])continue;
fa[to][0]=t,dep[to]=dep[t]+1;
dfs(to);
}
}
stack<int>S;
void tj(int t,int fr){
dfn[t]=low[t]=++dfc;
S.push(t);
for(auto ed:G[t]){
int to=u[ed]+v[ed]-t;
if(!dfn[to]){
tj(to,ed);
low[t]=min(low[t],low[to]);
}
else if(fr!=ed)low[t]=min(low[t],dfn[to]);
}
if(low[t]==dfn[t]){
cnt++;
while(S.top()!=t)edc[S.top()]=cnt,S.pop();
edc[S.top()]=cnt,S.pop();
}
}
int lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=19;i>=0;i--){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i=19;i>=0;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
void dfs2(int t){
bj[t]=1;
for(auto to:G2[t]){
if(bj[to])continue;
dfs2(to);
bj1[t]+=bj1[to],bj2[t]+=bj2[to];
}
if(bj1[t]&&bj2[t]){
cout<<"No";
exit(0);
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
cin>>u[i]>>v[i];
G[u[i]].pb(i),G[v[i]].pb(i);
}
for(int i=1;i<=n;i++){
if(!dfn[i])tj(i,0);
}
for(int i=1;i<=n;i++){
for(auto ed:G[i]){
int to=u[ed]+v[ed]-i;
if(edc[to]!=edc[i])G2[edc[i]].pb(edc[to]);
}
}
dfc=0;
for(int i=1;i<=cnt;i++){
if(!bj[i])dfc++,dep[i]=1,dfs(i);
}
for(int j=1;j<=19;j++){
for(int i=1;i<=cnt;i++){
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
while(k--){
int s,t;cin>>s>>t;
s=edc[s],t=edc[t];
if(bj[s]!=bj[t]){
cout<<"No";return 0;
}
if(s==t)continue;
int l=lca(s,t);
bj1[s]++,bj1[l]--,bj2[t]++,bj2[l]--;
}
memset(bj,0,sizeof bj);
for(int i=1;i<=cnt;i++){
if(!bj[i])dfs2(i);
}
cout<<"Yes";
return 0;
}
本文作者:Xdik
本文链接:https://www.cnblogs.com/Xdik/p/18713702
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步