cf555e(缩点)

给一个 n 个点 m 条边的图,以及 q 对点 (s,t),让你给 m 条边定向。问是否存在一种方案,使每对点的 s 能走到 t。 \(n,m,q≤ 2×10^5\).

首先,在一个边双内,一定存在一种定向方案,使得边双内点两两可达。(考虑桥)

因此,可以直接把边双缩点。然后树上差分看看有没有冲突即可。

注意rmq-st求lca用的是欧拉序!

#include <cstdio>
#include <functional>
#include <algorithm>
using namespace std; typedef pair<int, int> pi;
#define mp make_pair
const int maxn=2e5+5;
int n, m, q; struct Edge{
int fr, to, nxt;
}e1[maxn*2], e2[maxn*2];
int fir1[maxn], fir2[maxn], cnte1=1, cnte2=0;
void addedge(int x, int y, Edge *e, int *fir, int &cnte){
Edge &ed=e[++cnte]; ed.fr=x;
ed.to=y; ed.nxt=fir[x]; fir[x]=cnte; } int tim, dfn[maxn], low[maxn], st[maxn], tl, cntgp, gp[maxn];
void tarjan(int u, int pe, Edge *e, int *fir){ int v;
dfn[u]=low[u]=++tim; st[++tl]=u;
for (int i=fir[u]; i; i=e[i].nxt){
v=e[i].to; if (i==pe) continue; //对于求桥只能保存父边
if (dfn[v]){ low[u]=min(low[u], dfn[v]); continue; }
tarjan(v, i^1, e, fir); low[u]=min(low[u], low[v]);
}
if (dfn[u]!=low[u]) return;
++cntgp;
while (dfn[st[tl]]!=low[st[tl]]) gp[st[tl--]]=cntgp;
gp[st[tl--]]=cntgp;
} int block[maxn], cntb, dep[maxn], ftim[maxn], f[maxn*2][19], rt[maxn];
void st_dfs(int u, int p, Edge *e, int *fir){ int v;
block[u]=cntb; dep[u]=dep[p]+1; ftim[u]=++tim; f[tim][0]=u;
for (int i=fir[u]; i; i=e[i].nxt){
v=e[i].to; if (v==p) continue;
st_dfs(v, u, e, fir);
f[++tim][0]=u; //必须放在这里!!至于为什么,想想菊花(树)
}
} int up[maxn], down[maxn]; //标记类型:立即生效,而不是处理后生效 int getlca(int x, int y){ //注意如果x>y要swap
x=ftim[x]; y=ftim[y];
if (x>y) swap(x, y); int c=-1;
for (int l=y-x+1; l; l>>=1) ++c;
if (dep[f[x][c]]<dep[f[y-(1<<c)+1][c]]) return f[x][c];
else return f[y-(1<<c)+1][c];
} bool ans=true;
pi dfs(int u, int p, Edge *e, int *fir){
pi upi=mp(up[u], down[u]), tmp;
for (int i=fir[u]; i; i=e[i].nxt){
if (e[i].to==p) continue;
tmp=dfs(e[i].to, u, e, fir);
upi.first+=tmp.first; upi.second+=tmp.second;
}
if (upi.first&&upi.second) ans=false;
return upi;
} int main(){
scanf("%d%d%d", &n, &m, &q); int x, y, lca;
for (int i=1; i<=m; ++i){
scanf("%d%d", &x, &y);
addedge(x, y, e1, fir1, cnte1);
addedge(y, x, e1, fir1, cnte1); }
for (int i=1; i<=n; ++i) if (!dfn[i]) tarjan(i, 0, e1, fir1);
int g1, g2; tim=0;
for (int i=2; i<=cnte1; ++i)
if ((g1=gp[e1[i].fr])!=(g2=gp[e1[i].to]))
addedge(g1, g2, e2, fir2, cnte2);
for (int i=1; i<=cntgp; ++i)
if (!dep[i]) ++cntb, rt[i]=1, st_dfs(i, 0, e2, fir2);
for (int i=1; i<19; ++i)
for (int j=1; j<=tim; ++j){
if (j+(1<<i-1)>tim){ f[j][i]=f[j][i-1]; continue; }
if (dep[f[j][i-1]]<dep[f[j+(1<<i-1)][i-1]])
f[j][i]=f[j][i-1]; else f[j][i]=f[j+(1<<i-1)][i-1];
}
for (int i=1; i<=q; ++i){
scanf("%d%d", &x, &y); x=gp[x]; y=gp[y]; lca=getlca(x, y);
if (block[x]!=block[y]){ puts("No"); return 0; }
up[x]+=1; up[lca]+=-1; down[y]+=1; down[lca]+=-1;
}
for (int i=1; i<=cntgp; ++i)
if (rt[i]) dfs(i, 0, e2, fir2);
puts(ans?"Yes":"No");
return 0;
}

cf555e的更多相关文章

  1. CF555E Case of Computer Network

    题面:https://www.luogu.com.cn/problem/CF555E 题意:给定一张\(n\)个点\(m\)条边的无向图. 给定\(q\)组有向点对\((s,t)\). 询问是否存在使 ...

  2. 「CF555E」 Case of Computer Network

    「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...

  3. 题解 CF555E Case of Computer Network

    题目传送门 题目大意 给出一个\(n\)个点\(m\)条边的无向图,有\(q\)次有向点对\((s,t)\),问是否存在一种方法定向每条边使得每个点对可以\(s\to t\). \(n,m,q\le ...

  4. IOI 2020 集训队作业胡扯

    首先安慰自己:做的没集训队快很正常-- 很正常-- 做不完也很正常-- 很正常-- 全都不会做也很正常-- 很正常-- 表格 试题一 完成情况 试题二 完成情况 试题三 完成情况 cf549E cf6 ...

  5. IOI2020只因训队作业胡做

    w a r n i n g ! 意 识 流 警 告 !!1 不想一个个发了,干脆直接发个合集得了qwq 感觉这辈子都做不完了\(Q\omega Q\) CF516D 写过题解了 CF505E 写过题解 ...

  6. IOI 2020 国家集训队作业

    \(\checkmark\) 试题一 完成情况 试题二 完成情况 试题三 完成情况 cf549E cf674G arc103_f \(\checkmark\) cf594E agc034_f agc0 ...

随机推荐

  1. dd命令的高级应用

    dd是Linux上的一个常用的命令.例如:dd if=/1.txt of=/tmp/2.txt     (其中, if代表input file:of代表output file, 命令的结果就是将根目录 ...

  2. Centos6.6安装Python3.5笔录

    1.CentOS6.6 安装Python3.5 的依赖包 yum groupinstall "Development tools" yum install zlib-devel b ...

  3. hadoop中unhealthynodes的问题解决

    在yarn-site.xml中加入如下配置 <property>     <name>yarn.nodemanager.disk-health-checker.min-heal ...

  4. 【技术调研】最强Node-RED初探总结

    在某个项目中需要调研下node-red的功能,我大概花了三天时间研究了相关的官方文档,写了几个Demo总结了下node-red相关的功能.如需转载,请注明出处 https://www.cnblogs. ...

  5. 微信公众号php从0开发,包括功能(自定义菜单,分享)

    之前写的一篇微信公众号文章. 工作需要,进行此次调研,并记录开发过程. 开发目的,页面授权,页面获取用户头像,用户昵称 微信id, 分享页面. 微信订阅号 无法获取用户个人信息 写在记录前,公众号也是 ...

  6. 虚拟机之 LNMP

    LNMP就是Linux nginx mysql php 一.mysql 下载安装mysql转至 LAMP (点击“LAMP”即可跳转) 也可以从快照跳转至mysql安装ok 二.php 下载同上, 1 ...

  7. PyInstaller打包成exe可执行文件

    PyInstaller 安装pyinstaller 对于那些网络比较稳定,能够流畅使用pip源地址的用户,直接下面的命令就可以搞定: pip install pyinstaller 通常我们会下载源码 ...

  8. Holding Bin-Laden Captive!

    We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...

  9. C51串口的SCON寄存器及工作…

    原文地址:C51串口的SCON寄存器及工作方式作者:batistar 一,串行口控制寄存器SCON 它用于定义串行口的工作方式及实施接收和发送控制.字节地址为98H,其各位定义如下表: D7 D6 D ...

  10. sublime添加右键菜单

    参考:https://www.zhihu.com/question/29662273/answer/45277925 @echo Off :START CLS echo *============== ...