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. Windows修改MySQL用户root密码

    MySQL是一个关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软 ...

  2. C++深度解析教程学习笔记(5)面向对象

    1. 面向对象基本概念 (1)面向对象的意义在于 ①将日常生活中习惯的思维方式引入程序设计中 ②将需求中的概念直观的映射到解决方案中 ③以模块为中心构建可复用的软件系统 ④提高软件产品的可维护性和可扩 ...

  3. scala中存在的问题

    2017-12-27 scala学习中存在的问题: 1.表达式的概念要搞清楚 2.八种基本数据类型要搞清楚 Byte\Short\Int\Long\Char\Boolean\Double\Float都 ...

  4. form表单中的button按钮

    如果在form表单中 , 存在button元素,button元素就充当了submit的角色

  5. Android 创建项目出现No resource found that matches the given name Theme.AppCompat.Light

    关于为何出现No resource found that matches the given name ‘Theme.AppCompat.Light’的原因 这边博客已经写的很清楚了 大家可以参考一下 ...

  6. 在ubuntu12.04上安装6款顶级漂亮的BURG主题

    BURG 基本上是一个基于GRUB的Linux引导装载程序.BURG格有一个高度可配置的菜单系统,可选择文本和图形模式.简而言之,BURG可广泛定制,有良好免费的BURG主题.选择自己最喜欢的,下面我 ...

  7. Win10系统优化/设置脚本

    Win10系统优化/设置脚本 用了很长时间win10了,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win10的系统设置,结合DWS对Win1 ...

  8. C++获取时间并命名为文件名

    #include <time.h> char pStrPath1[20];time_t currTime;struct tm *mt;Mat saveImg1; currTime = ti ...

  9. mybatis的执行流程 #{}和${} Mysql自增主键返回 resultMap 一对多 多对一配置

    n Mybatis配置 全局配置文件SqlMapConfig.xml,配置了Mybatis的运行环境等信息. Mapper.xml文件即Sql映射文件,文件中配置了操作数据库的Sql语句.此文件需要在 ...

  10. ssh -X前设置DISPLAY=localhost:0

    如果是在windows上用XMing做XServer,前面的localhost不能省,否则会被当作一个unix domain socket,而XMing没有实现这个功能,所以会出错 connect / ...