CF555E Case of Computer Network
题面:https://www.luogu.com.cn/problem/CF555E
题意:给定一张\(n\)个点\(m\)条边的无向图。
给定\(q\)组有向点对\((s,t)\)。
询问是否存在使得所有\(s\)都能到达\(t\)的无向图中每条边的定向方案。
n,m,q \(\leq\) 2e5
题解:
看到这种关于无向图连通性的问题,我们可以想到边双。
如果两点在边双中,我们可以将边双里的边定向为一些环,
这样这些点一定是可以到达的。
考虑边双缩点,将整张图缩成一个森林。
接下来,对于每对\((s,t)\),我们树上差分记录一下
每条边的方向,最后DFS判断一下就行了。
时间复杂度\(O(n+m)\)
代码:
#include<bits/stdc++.h>
using namespace std;
#define re register int
#define F(x,y,z) for(re x=y;x<=z;x++)
#define FOR(x,y,z) for(re x=y;x>=z;x--)
typedef long long ll;
#define I inline void
#define IN inline int
#define STS system("pause")
template<class D>I read(D &res){
res=0;register D g=1;register char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')g=-1;
ch=getchar();
}
while(isdigit(ch)){
res=(res<<3)+(res<<1)+(ch^48);
ch=getchar();
}
res*=g;
}
struct E{
int to,nt;
}e[404000];
#define T e[k].to
int n,m,Q,tot,X,Y,sn,head[202000];
int dfn[202000],low[202000],clr[202000],color,mill,vis[202000];
int dep[202000],fa[202000],f[202000][20],lg[202000],top[202000];
int up[202000],down[202000];
stack<int>s;
vector<int>v[202000];
vector<int>::iterator it;
I tarjan(int x,int pre){
dfn[x]=low[x]=++mill;
s.push(x);vis[x]=1;
for(re k=head[x];k!=-1;k=e[k].nt){
if(k==(pre^1))continue;
if(!dfn[T]){
tarjan(T,k);
low[x]=min(low[x],low[T]);
}
else if(vis[T])low[x]=min(low[x],dfn[T]);
}
if(dfn[x]==low[x]){
++color;
while(s.top()!=x){clr[s.top()]=color;vis[s.top()]=0;s.pop();}
clr[x]=color;vis[x]=0;s.pop();
}
}
I D_1(int x,int fat,int depth,int topi){
//cout<<x<<":";
f[x][0]=fat;top[x]=topi;dep[x]=depth;
F(i,1,lg[dep[x]])f[x][i]=f[f[x][i-1]][i-1];
for(auto k:v[x]){
//cout<<k<<" ";
if(k==fat)continue;
D_1(k,x,depth+1,topi);
}
//cout<<endl;
}
I add(int x,int y){v[x].emplace_back(y);}
IN ques_lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
re len=dep[x]-dep[y];
FOR(i,lg[len],0){
if((len>>i)&1)x=f[x][i];
}
if(x==y)return x;
FOR(i,lg[dep[x]],0){
if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
}
return f[x][0];
}
I D_3(int x,int fat){
if(!sn)return;
for(auto k:v[x]){
if(k==fat)continue;
D_3(k,x);
if(!sn)return;
up[x]+=up[k];down[x]+=down[k];
}
//cout<<x<<" "<<up[x]<<" "<<down[x]<<endl;
if(up[x]&&down[x])sn=0;
}
int main(){
read(n);read(m);read(Q);tot=-1;
memset(head,-1,sizeof(head));
F(i,1,m){
read(X);read(Y);
e[++tot].to=Y;
e[tot].nt=head[X];
head[X]=tot;
e[++tot].to=X;
e[tot].nt=head[Y];
head[Y]=tot;
}
F(i,1,n){
if(!dfn[i])tarjan(i,-1);
}
F(i,1,n){
for(re k=head[i];k!=-1;k=e[k].nt){
if(clr[i]!=clr[T])add(clr[i],clr[T]);
}
}
lg[0]=-1;
F(i,1,n)lg[i]=lg[i>>1]+1,top[i]=i;
F(i,1,color){
sort(v[i].begin(),v[i].end());
it=unique(v[i].begin(),v[i].end());
v[i].erase(it,v[i].end());
//cout<<i<<":";
//for(auto k:v[i])cout<<k<<" ";
//cout<<endl;
}
F(i,1,color){
if(top[i]==i)dep[i]=1,D_1(i,0,1,i);
}//STS;
re P;sn=1;
while(Q--){
read(X);read(Y);X=clr[X];Y=clr[Y];
if(top[X]!=top[Y]){cout<<"No";return 0;}
P=ques_lca(X,Y);//cout<<X<<" "<<Y<<" "<<P<<endl;
up[X]++;down[Y]++;up[P]--;down[P]--;
}
//F(i,1,color)cout<<up[i]<<" "<<down[i]<<endl;
F(i,1,color){
if(top[i]==i)D_3(i,0);
if(!sn)break;
}
if(sn)cout<<"Yes";
else cout<<"No";
return 0;
}
/*
8 13 4
5 3
2 7
5 8
2 4
6 4
2 6
5 8
1 4
6 7
3 1
6 7
3 1
8 3
8 7
8 7
5 1
6 8
*/
CF555E Case of Computer Network的更多相关文章
- 题解 CF555E Case of Computer Network
题目传送门 题目大意 给出一个\(n\)个点\(m\)条边的无向图,有\(q\)次有向点对\((s,t)\),问是否存在一种方法定向每条边使得每个点对可以\(s\to t\). \(n,m,q\le ...
- 「CF555E」 Case of Computer Network
「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- (中等) CF 555E Case of Computer Network,双连通+树。
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...
- 555E Case of Computer Network
分析 一个连通块内的肯定不影响 于是我们先缩点 之后对于每个路径 向上向下分别开一个差分数组 如果两个数组同时有值则不合法 代码 #include<bits/stdc++.h> using ...
- Solution -「CF 555E」Case of Computer Network
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
随机推荐
- 传统IO拷贝与零拷贝技术比较
1. 传统IO 由上面图知,传统io需要经过4次copy, 3次状态切换 第一次: 从硬盘 经过 DMA 拷贝 到 kernel buffer (内核buferr) 第二次: 从kernel buff ...
- Jmeter参数化控件意见收集
1.可以读取EXCEL,可以自定义SHEET,行和列: 2.数据可以加密传输,加密方式如下: 1)SHA1 2)SHA224 3)SHA256 4)SHA384 5)SHA512 6)MD5 7)Hm ...
- CoreData新增字段
1. 在模型文件的Entity里面增加字段名 2. Xcode工具栏选择Edtor->Creat NSManagerObject SubClass->...->生成新的关联文件 3. ...
- Sending forms through JavaScript[form提交 form data]
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript As in the ...
- PHP csv导出数据 (二)
全部导出和时间导出 html代码,全程并不需要引用什么插件 <include file="public@header"/> <link href="__ ...
- random、os、时间模块
一.random 模块 1.随机小数 random.random() #产生大于0且小于1之间的小数 random.uniform(1,3) #产生1到3之间的随机小数 2.随机整数 rand ...
- JS-Number 的精度
JS 使用 IEEE 754 的双精度数表示数字,1 位符号,10 位指数,53 位底数. 所以 JS 数字精度近似为 15.95 位 10 进制(10 ** 15.95). 也就是说整部加小数部分超 ...
- Vagrant 手册之 box - box 的文件格式
原文地址 过去,box 只是 VirtualBox 导出的 tar 文件.由于 Vagrant 现在支持多个 provider 和版本控制,box 文件稍微复杂一些. 用于 Vagrant 1.0.x ...
- JSP中四种属性保存范围(2)
1.session <%@ page language="java" contentType="text/html" pageEncoding=" ...
- 关于public private protected访问修饰符
这个似乎都是老生常谈了,特别是找工作第一轮笔试的时候很爱考这些,再罗列一次,特别要注意继承的情况: 默认状态:即是不加修饰符的时候,所谓的default状态,在类内部可以被访问,在相同的包下面 ...