题目传送门

题目大意

给出一个\(n\)个点\(m\)条边的无向图,有\(q\)次有向点对\((s,t)\),问是否存在一种方法定向每条边使得每个点对可以\(s\to t\)。

\(n,m,q\le 2\times 10^5\)

思路

首先我们可以发现,一个边双连通分量里面肯定可以满足,因为任意两点之间都有两条及以上路径,于是可以一条过去,一条回来。于是,我们就可以先双连通分量缩点一下。

接着我们发现缩点完之后一定是个森林,因为如果存在环的话一定还可以缩点。那我们就可以用树上差分解决这个问题了。具体的话对于点对\((s,t)\),显然\(s\to lca(s,t)\)应该是向上的,\(lca(s,t)\to t\)应该是向下的。如果存在一个边既向上又向下那显然是矛盾的。

于是,我们就可以在\(\Theta(n\log n)\)的时间复杂度内解决这个问题。(求lca)

\(\texttt{Code}\)

#include <bits/stdc++.h>
using namespace std; #define Int register int
#define MAXN 400005 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} bool vis[MAXN << 1];
int n,m,q,s[MAXN],t[MAXN],sum[MAXN],dfn[MAXN],low[MAXN];
struct SolveTree{
int up;//表示边双连通分量有多少个
struct edge{
int v,nxt;
}e[MAXN << 1];
int toop = 1,Index,s1[MAXN],s2[MAXN],fa[MAXN][21],col[MAXN],dep[MAXN],head[MAXN];
void Add_Edge (int u,int v){
e[++ toop] = edge {v,head[u]};head[u] = toop;
e[++ toop] = edge {u,head[v]};head[v] = toop;
}
void dfs (int u,int par){
col[u] = Index,fa[u][0] = par,dep[u] = dep[par] + 1;
for (Int i = 1;i <= 20;++ i) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (Int i = head[u];i;i = e[i].nxt){
int v = e[i].v;
if (v ^ par) dfs (v,u);
}
}
int LCA (int x,int y){
if (dep[x] < dep[y]) swap (x,y);
for (Int i = 20,dis = dep[x] - dep[y];~i;-- i) if (dis >> i & 1) x = fa[x][i];
if (x == y) return x;
else{
for (Int i = 20;~i;-- i) if (fa[x][i] ^ fa[y][i]) x = fa[x][i],y = fa[y][i];
return fa[x][0];
}
}
bool Remain(int u){
for (Int i = head[u];i;i = e[i].nxt){
int v = e[i].v;
if (v ^ fa[u][0]){
if (!Remain (v) || (s1[v] && s2[v])) return 0;
s1[u] += s1[v],s2[u] += s2[v];
}
}
return 1;
}
void Solve (){
for (Int i = 1;i <= up;++ i) if (!col[i]) ++ Index,dfs (i,0);
for (Int i = 1;i <= q;++ i){
int u = s[i],v = t[i];
if (col[u] != col[v]) return puts ("No"),void ();
else{
int w = LCA (u,v);
s1[u] ++,s1[w] --,s2[v] ++,s2[w] --;
}
}
for (Int i = 1;i <= up;++ i) if (dep[i] == 1 && !Remain (i)) return puts ("No"),void ();
puts ("Yes");
}
}T;
struct CutTree{
struct edge{
int v,nxt;
}e[MAXN << 1];
int top = 1,Index,head[MAXN],col[MAXN];
void Add_Edge (int u,int v){
e[++ top] = edge {v,head[u]};head[u] = top;
e[++ top] = edge {u,head[v]};head[v] = top;
}
int id;
void Tarjan (int u,int fa){
dfn[u] = low[u] = ++ id;
for (Int i = head[u];i;i = e[i].nxt) if (e[i].v ^ fa){
int v = e[i].v;
if (!dfn[v]){
Tarjan (v,u),low[u] = min (low[u],low[v]);
if (low[v] > dfn[u]) vis[i] = vis[i ^ 1] = 1;//vis[i]表示该边是不是割边
}
else low[u] = min (low[u],dfn[v]);
}
}
void dfs (int u){
col[u] = Index;
for (Int i = head[u],v;i;i = e[i].nxt) if (!col[v = e[i].v] && !vis[i]) dfs (v);
}
void build (){
read (n,m,q);
for (Int i = 1,u,v;i <= m;++ i) read (u,v),Add_Edge (u,v);
for (Int i = 1;i <= n;++ i) if (!dfn[i]) Tarjan (i,0);
for (Int i = 1;i <= n;++ i) if (!col[i]) ++ Index,dfs (i);T.up = Index;
for (Int i = 1;i <= n;++ i) for (Int j = head[i],v;j;j = e[j].nxt) if (col[v = e[j].v] > col[i]) T.Add_Edge (col[i],col[v]);
for (Int i = 1;i <= q;++ i){
read (s[i],t[i]),s[i] = col[s[i]],t[i] = col[t[i]];
if (s[i] == t[i]) i --,q --;
}
}
}G; signed main(){
G.build(),T.Solve();
return 0;
}

题解 CF555E Case of Computer Network的更多相关文章

  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. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  4. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  5. 555E Case of Computer Network

    分析 一个连通块内的肯定不影响 于是我们先缩点 之后对于每个路径 向上向下分别开一个差分数组 如果两个数组同时有值则不合法 代码 #include<bits/stdc++.h> using ...

  6. Solution -「CF 555E」Case of Computer Network

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...

  7. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  8. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  9. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

随机推荐

  1. C# - 习题05_写出程序的输出结果o1.count

    时间:2017-08-24 整理:byzqy 题目:写出下列程序的输出结果: //原题程序如下: class Class1 { private static int count = 0; static ...

  2. Ubuntu 16.04 NVidia显卡 输入密码后 重复出现登录界面

    问题根源:显卡驱动 解决办法: CTRL+ALT+F1 # 切换到命令行 sudo service lightdm stop  # 关闭桌面显示管理器 sudo apt-get remove --pu ...

  3. unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source 解决办法

    Project -> Properties -> C/C++ -> Precompiled Headers -> Precompiled Header -> 选择Not ...

  4. 小白2分钟学会Visual Studio将引用包打包到NuGet上

    前言 之前我们说 10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#),过程有些许繁琐,并且需要有一定的基础. 怎么办呢,不慌,看法宝. 两 ...

  5. 硕盟SM-H2V1 HDMI转VGA 笔记本台式主机HDMI转VGA显示器转接头

    硕盟SM-G2V1  HDMI转VGA高清转换器一款采用优质芯片的HDMI转VGA转换器,快速传输众享1080P的高清画质显示,而且HDMI转VGA高清转换器,采用24k镀金工艺,耐磨.耐腐蚀性强,这 ...

  6. C# 获得当前方法 和 方法调用链 的 方法

    一个获得方法名的方法,depth表示调用此方法的回溯深度. 比如,A方法调用B方法,B方法调用GetCurrentMethodFullName(2),那么得到的结果是A方法的全名(namespace+ ...

  7. 案例九:shell脚本自动创建多个新用户,并设置密码

    此脚本是用来批量创建用户并设置用户密码,在企业用非常实用. 脚本一 #!/bin/bash for name in $( seq 1 100 ) do useradd "user$name& ...

  8. 3.15学习总结(Python爬取网站数据并存入数据库)

    在官网上下载了Python和PyCharm,并在网上简单的学习了爬虫的相关知识. 结对开发的第一阶段要求: 网上爬取最新疫情数据,并存入到MySql数据库中 在可视化显示数据详细信息 项目代码: im ...

  9. 使用OPCache提升PHP的性能

    对于 PHP 这样的解释型语言来说,每次的运行都会将所有的代码进行一次加载解析,这样一方面的好处是代码随时都可以进行热更新修改,因为我们不需要编译.但是这也会带来一个问题,那就是无法承载过大的访问量. ...

  10. 创建一个 Orchard Core CMS 站点

    本文通过引用项目模板的方式创建Orchard CMS站点. 创建项目有不同的方式可以为Orchard Core创建站点和模块.你可以在这里了解更多关于它们的信息.在本指南中,我们将使用我们的" ...