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 ...
随机推荐
- 写php用什么编辑器
编辑器是编程工作者强有力的工具,一款好的编辑器可以大大加快程序员的开发速度.那么,如何在众多编辑器中选出顺手的编辑器呢? 下面为大家推荐几款好评较多的编辑器: 1.NetBeans —— 免费,开源, ...
- BZOJ 4836: [Lydsy1704月赛]二元运算 分治FFT
Code: #include<bits/stdc++.h> #define ll long long #define maxn 500000 #define setIO(s) freope ...
- vue项目适应不同屏幕做的适配器
一般宽度是1920的,但是有的电脑屏幕很窄,导致页面样式错乱,那么可以设置app.vue以及主页面里的样式宽度为1920px,超过了就auto. 如下: (app.vue) (home.vue) 原效 ...
- Linux下MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)
一.登录MySQL查看用SHOW VARIABLES LIKE 'character%';下字符集,显示如下: +--------------------------+---------------- ...
- Jenkins+Gitlab+自动化测试配置持续集成
Jenkins安装在win7上 GitLab安装在docker上 需求:本地提交自动化测试代码在gitlab上后,jenkins自动构建,拉下新提交的自动化代码,并且运行 参考的链接: https:/ ...
- js-jssdk微信H5选择多张图片预览并上传(兼容ios,安卓,已测试)
值得注意的是: 1.在微信H5中选择图片运用:wx.chooseImage,成功后返回: res.localIds用于上传图片使用 上传图片:wx.uploadImage. 2.上传图片的时候 ...
- HTML--JS 二级联动
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- java.lang.NumberFormatException: For input string: "title"异常
java.lang.NumberFormatException: For input string: "title" at java.lang.NumberFormatExcept ...
- jmeter:清除本地指定目录下的所有类型文件
1,创建一个sampler 2,要在本地有一个目录的文件 3,直接上代码 String path = "C:\\临时文件\\test111" ; File file ...
- mysql 5.7 创建函数报错,This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creat
今天用命令创建函数, 报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration ...