Problem Description

TC (Tian Chao) is magical place, as you all know...
The railways and the rail-stations in TC are fragile and always meet with different kinds of problems. In order to reach the destination safely on time, you are asked to develop a system which has two types of main functions as below.
1: A B C D, reporting whether we can get from station A to station B without passing the railway that connects station C and station D.
2: A B C, reporting whether we can get from station A to station B without passing station C.
Please notice that the railways are UNDIRECTED.
 

Input

For each test case, the first line will contain two integers N (2<=N<=100000) and M (1<=M<=500000), namely the number of stations and railways in TC. Then each of the next M lines will have two integers, describing the two stations that a certain railway is connecting. After this, there comes a line containing a single integer Q (Q<=300000), which means the number of queries to make on the system. The next Q lines will be queries. Each query begins with a integer, indicating the type of query, followed by 4 (the first type) or 3 (the second type) integers describing the details of the query as what mentioned above.
The stations are always labeled from 1 to N.
 

Output

For each test case, print "yes" or "no" in separated lines for the queries.
 
Sample Input
13 15
1 2
2 3
3 5
2 4
4 6
2 6
1 4
1 7
7 8
7 9
7 10
8 11
8 12
9 12
12 13
5
1 5 13 1 2
1 6 2 1 4
1 13 6 7 8
2 13 6 7
2 13 6 8
Sample Output
yes
yes
yes
no
yes
 
Source
 
Recommend
We have carefully selected several similar problems for you:  3894 3891 3892 3895 3899 
 

题解:
无向图,n个点,m条边;
typ==1:
  a,b,c,d : 判断去掉c和d之间的边之后a,b是否联通;
typ==2:
  a,b,c:去掉c点之后,a,b是否仍然联通;
思路:先dfs出每个点的dfs序,并处理出每个点的深度以及是否为割点,每条边是否为割边;
对于第一种:(假设c在d的下面)如果a和b一个在c的子树里面,一个不再c的子树里面,并且dc边是桥,则不连通,其他均为联通;
对于第二种:

分成三种情况讨论:

    1: a,b都在子树c中,如果a,b在c的同一个儿子当中,那么去掉c是联通的;否则让a,b往上跳,变成c的两个儿子,如果lown(a)>=dfn(c) 或 lown(b)>=dfn(c)成立,那么不连通;

  2:a,b只又一个在子树c中,假设a在子树c中,那么,同样让a往上跳,变成c的儿子,.如果lown[a]>=dfn[c],那么不连通,否则联通;

  3:a,b都不在子树c中,那么去掉c没有任何影响,所以还是联通(往上跳,可以用倍增法);

 
参考带码:
#include<bits/stdc++.h>
using namespace std;
#define mod 10007
#define pii pair<int,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define mkp make_pair
#define PI acos(-1.0)
typedef long long ll;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
inline ll readll()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} const int maxn=1e5+;
const int maxm=5e5+; struct Edge{
int u,v;
int nxt;
} edge[maxm<<];
int n,m,head[maxn],tot,times;
int fa[maxn],dep[maxn],dfn[maxn],out[maxn],lown[maxn];
bool iscut[maxn],isbridge[maxn];
int anc[maxn][];
inline void AddEdge(int u,int v)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].nxt=head[u];
head[u]=tot++;
} inline void Init()
{
tot=times=;
memset(head,-,sizeof(head));
memset(iscut,false,sizeof(iscut));
memset(isbridge,false,sizeof(isbridge));
memset(dep,,sizeof(dep));
memset(fa,,sizeof(fa));
} inline void dfs(int u)
{
dfn[u]=lown[u]=++times;
bool flag=false;
int child=;
for(int e=head[u];~e;e=edge[e].nxt)
{
int v=edge[e].v;
if(v==fa[u]) continue;
if(!dfn[v])
{
fa[v]=u;
dep[v]=dep[u]+;
dfs(v);
lown[u]=min(lown[u],lown[v]);
if(lown[v]>=dfn[u])
{
iscut[u]=true;
if(lown[v]>dfn[u]) isbridge[v]=true;
}
}
else lown[u]=min(lown[u],dfn[v]);
}
if(u== && child==) iscut[u]=false;
out[u]=times;
} inline bool subtree(int x,int y)
{
return (dfn[x]>=dfn[y]&&out[x]<=out[y])?:;
} inline void preprocess()
{
memset(anc,,sizeof(anc));
for(int i=;i<=n;++i) anc[i][]=fa[i];
for(int j=;(<<j)<n;++j)
for(int i=;i<=n;++i)
if(anc[i][j-]) anc[i][j]=anc[anc[i][j-]][j-];
} inline int upward(int u, int x)
{
for(int i=;i<;i++)
if((x>>i)&) u=anc[u][i];
return u;
} inline bool Judge(int a,int b,int c)
{
int in1=subtree(a,c);
int in2=subtree(b,c);
if(in1&in2)
{
a=upward(a,dep[a]-dep[c]-);
b=upward(b,dep[b]-dep[c]-);
if(a==b) return true;
if(lown[a]>=dfn[c]||lown[b]>=dfn[c]) return false;
}
if(in1^in2)
{
if(!in1) swap(a,b);
a=upward(a,dep[a]-dep[c]-);
if(lown[a]>=dfn[c]) return false;
}
return true;
} int main()
{
scanf("%d%d",&n,&m);
Init();
int u,v;
for(int i=;i<=m;++i)
{
u=read(),v=read();
AddEdge(u,v);AddEdge(v,u);
}
dfs();preprocess();
int q=read();
while(q--)
{
int typ,a,b,c,d;
typ=read();a=read();b=read();c=read();
if(typ==)
{
d=read();
if(dep[c]<dep[d]) swap(c,d);
int temp1=subtree(a,c);
int temp2=subtree(b,c);
if(isbridge[c]&&(temp1^temp2)) puts("no");
else puts("yes");
}
else
{
bool ok=Judge(a,b,c);
if(ok) puts("yes");else puts("no");
}
}
return ;
}
 
 
 

HDU3896 Greatest TC(双联通分量+倍增)的更多相关文章

  1. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

  2. 『Tarjan算法 无向图的双联通分量』

    无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...

  3. 【UVA10972】RevolC FaeLoN (求边双联通分量)

    题意: 给你一个无向图,要求把所有无向边改成有向边,并且添加最少的有向边,使得新的有向图强联通. 分析: 这题的解法还是很好想的.先用边双联通分量缩点,然后找新图中入度为0和为1的点,入度为0则ans ...

  4. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  5. HDU5409---CRB and Graph 2015多校 双联通分量缩点

    题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...

  6. poj2942(双联通分量,交叉染色判二分图)

    题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 思路:首先 ...

  7. 大白书中无向图的点双联通分量(BCC)模板的分析与理解

    对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...

  8. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  9. ARC062 - F. Painting Graphs with AtCoDeer (Polya+点双联通分量)

    似乎好久都没写博客了....赶快来补一篇 题意 给你一个 \(n\) 个点 , 没有重边和自环的图 . 有 \(m\) 条边 , 每条边可以染 \(1 \to k\) 中的一种颜色 . 对于任意一个简 ...

随机推荐

  1. 轻松实现C/C++各种常见进制相互转换

    其它进制转为十进制 在实现这个需求之前,先简单介绍一个c标准库中的一个函数: long strtol( const char *str, char **str_end, int base); 参数详细 ...

  2. 领扣(LeetCode)独特的电子邮箱地址 个人题解

    每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母,这些电 ...

  3. Javascript脚本语言

    找组件用 id (唯一) 2.name 样式 使用分类 1 页面中 2 建JS文件 可以放在head也可以在body 工作区可以有 1 全局变量 2 由多个函数构成 标签编辑器 onChange 改变 ...

  4. 就该这样理解 OSI 七层参考模型、浅谈不同局域网之间的通信

    简介 说到OSI参考模型,理解网络与网络之间的关系,不说太深入难以理解的东西,只求能最大程度上理解与使用. 参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一般称为O ...

  5. Video的自我学习

    直播原理 视频协议 HLS协议  [主要是直播方面(好用,但延时)] HTTP Live Streaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议. 是苹果公司Quic ...

  6. [FPGA]Verilog实现寄存器LS374

    目录 想说的话... 正文 IC介绍 电路连接图 功能表 逻辑图 实验原理 单元实现_D触发器 整体实现(完整代码) 想说的话... 不久前正式开通了博客,以后有空了会尽量把自己学习过程中的心得或者感 ...

  7. robatframework+jenkins+email集成部署方案

    准备工作: 1.jenkins.war包 下载地址:https://jenkins.io/zh/download/ 2.Jdk1.8 下载地址:http://www.oracle.com/techne ...

  8. Nginx 代理本地文件夹(Windows环境)

    安装环境: win10 nginx-1.17.2 步骤: 一.打开nginx.conf 路径:\nginx-1.17.2\conf\nginx.conf 二.编辑 配置跨域以及代理文件夹路径 三.启动 ...

  9. 报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    问题: 在写windows服务时,发布后日志报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Oracle 客户端组件的情况下以 64 ...

  10. WPF 修改屏幕DPI,会触发控件重新加载Unload/Load

    修改屏幕DPI,会触发控件的Unloaded/Loaded 现象/重现案例 对Unloaded/Loaded的印象: FrameworkElement, 第一次加载显示时,会触发Loaded.元素被释 ...