555E Case of Computer Network
分析
一个连通块内的肯定不影响
于是我们先缩点
之后对于每个路径
向上向下分别开一个差分数组
如果两个数组同时有值则不合法
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,q,bl[],s[],t[],dfn[],low[],is[];
int cnt,tot,sum,head[],nxt[],to[],ano[];
int d1[],d2[],dep[],pr[][];
int f[],vis[],vis2[];
vector<int>v[];
stack<int>a;
inline int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
inline void add(int x,int y){
nxt[++tot]=head[x],head[x]=tot,to[tot]=y,ano[tot]=tot+;
nxt[++tot]=head[y],head[y]=tot,to[tot]=x,ano[tot]=tot-;
}
inline void tarjan(int x,int id){
dfn[x]=low[x]=++cnt;
a.push(x),is[x]=;
for(int i=head[x];i;i=nxt[i])
if(i!=ano[id]){
if(!dfn[to[i]]){
tarjan(to[i],i);
low[x]=min(low[x],low[to[i]]);
}else if(is[to[i]]){
low[x]=min(low[x],dfn[to[i]]);
}
}
if(low[x]==dfn[x]){
sum++;
while(){
int u=a.top();
a.pop(),is[u]=;
bl[u]=sum;
if(u==x)break;
}
}
}
inline void dfs(int x,int fa){
vis2[x]=;
pr[x][]=fa;
dep[x]=dep[fa]+;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa)dfs(v[x][i],x);
}
inline int lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
int i,j,k,len=dep[x]-dep[y];
for(i=;i<=;i++)
if((<<i)&len)x=pr[x][i];
if(x==y)return x;
for(i=;i>=;i--)
if(pr[x][i]!=pr[y][i])
x=pr[x][i],y=pr[y][i];
return pr[x][];
}
inline void dfs2(int x,int fa){
vis[x]=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa)dfs2(v[x][i],x),d1[x]+=d1[v[x][i]],d2[x]+=d2[v[x][i]];
if(d1[x]>&&d2[x]>){
puts("No");
exit();
}
}
int main(){
int i,j,k;
scanf("%d%d%d",&n,&m,&q);
for(i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
if(sf(x)!=sf(y))f[sf(x)]=sf(y);
}
for(i=;i<=q;i++)scanf("%d%d",&s[i],&t[i]);
for(i=;i<=q;i++)if(sf(s[i])!=sf(t[i])){puts("No");return ;}
for(i=;i<=n;i++)if(!dfn[i])tarjan(i,);
for(i=;i<=n;i++)
for(j=head[i];j;j=nxt[j])
if(bl[i]!=bl[to[j]])v[bl[i]].push_back(bl[to[j]]);
for(i=;i<=sum;i++)if(!vis2[i])dfs(i,);
for(i=;i<=;i++)
for(j=;j<=sum;j++)
pr[j][i]=pr[pr[j][i-]][i-];
for(i=;i<=q;i++){
if(bl[s[i]]==bl[t[i]])continue;
int x=bl[s[i]],y=bl[t[i]],z=lca(x,y);
d1[x]++,d1[z]--;
d2[y]++,d2[z]--;
}
for(i=;i<=sum;i++)if(!vis[i])dfs2(i,);
puts("Yes");
return ;
}
555E 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 ...
- 「CF555E」 Case of Computer Network
「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...
- Solution -「CF 555E」Case of Computer Network
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...
- CF555E Case of Computer Network
题面:https://www.luogu.com.cn/problem/CF555E 题意:给定一张\(n\)个点\(m\)条边的无向图. 给定\(q\)组有向点对\((s,t)\). 询问是否存在使 ...
- 题解 CF555E Case of Computer Network
题目传送门 题目大意 给出一个\(n\)个点\(m\)条边的无向图,有\(q\)次有向点对\((s,t)\),问是否存在一种方法定向每条边使得每个点对可以\(s\to t\). \(n,m,q\le ...
- 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 ...
随机推荐
- 企业级开发账号In House ipa发布流程
这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号.在网上查了一下资料,感觉没有一个比较完善的流程,于是决定把整个流程写下来,供大家参考. 首先详细说明一下我们的目标,我们需要发布 ...
- C++循环单链表删除连续相邻重复值
比如:1(头)->2->2->3->3->1->1(头) 去除以后的结果是1->2->3,注意头尾的1也要去掉一个. #include "st ...
- 安装superset踩过的坑
问题一: # fabmanager create-admin --app superset setuptools_scm.version.SetuptoolsOutdatedWarning: your ...
- “12306”是如何支撑百万QPS的?
来源:掘金 作者:绘你一世倾城 链接:https://juejin.im/post/5d84e21f6fb9a06ac8248149 秒杀系统的艺术 12306抢票,极限并发带来的思考? 每到节假日期 ...
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...
- PIL 中的 Image 模块
转载:http://www.cnblogs.com/way_testlife/archive/2011/04/20/2022997.html PIL 中的 Image 模块 本文是节选自 PIL ...
- dubbo 2.8.4(dubbox)的jar包制作【添加到maven本地仓库】
1. 下载 网址:https://github.com/hongmoshui/dubbox 2. 解压zip文件 3. 用maven编译文件 如果没有配置全局maven,就直接使用cmd命令行[进 ...
- VMware主机使用无线上网
VMware主机使用无线上网,默认的NAT连接在ubuntu下上不了网,需要把网络适配器改成桥接模式.
- Spring Boot JPA - Querydsl
https://lufficc.com/blog/spring-boot-jpa-querydsl