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 ...
随机推荐
- 英语单词debug
来源——docker官网首页https://www.docker.com/ 翻译 vt. 调试,排除故障,除错,改正有毛病部分 过去式 debugged 过去分词 debugged 现在分词 debu ...
- C#第一个程序Helloworld
- Codeforces 846F - Random Query
原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...
- DataTable 转Json格式C#代码
/// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...
- linux RZ 命令
root 账号登陆后,依次执行以下命令: 1 cd /tmp 2 wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz 3 tar zxv ...
- org.dom4j 解析XML
org.dom4j 解析xml java 代码 1 import java.io.File; import java.io.FileOutputStream; import java.io.FileW ...
- 永远让比较函数对相等的值返回false
今天在刷OJ的时候,有一道题一直Runtime Error,查错出来是比较函数写挂掉了,但是不知道错误在哪,于是查阅资料:永远让比较函数对相等的值返回false 具体可点击此处查看分析:链接 另外,在 ...
- VS2012生成Web时报未能找到元数据文件xxx.dll
问题:引用里已经添加了,还是报‘未能找到元数据文件xxx.dll’ 解决:添加了相同的不同路径的xxx.dll文件,删掉一个用不到的,就不报错了
- MySQL基础(创建库,创建表,添加数据)
CREATE DATABASE 数据库名; CREATE TABLE student2(sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号&quo ...
- [HDU 1529]Cashier Employment(差分约束系统)
[HDU 1529]Cashier Employment(差分约束系统) 题面 有一个超市,在24小时对员工都有一定需求量,表示为\(r_i\),意思为在i这个时间至少要有i个员工,现在有n个员工来应 ...