H - Rescue the Princess ZOJ - 4097 (tarjan缩点+倍增lca)
题目链接:
H - Rescue the Princess
学习链接:
zoj4097 Rescue the Princess无向图缩点有重边+lca - lhc..._博客园
题目大意:
首先是T组测试样例,然后是n个点,m条双向边。然后给你u,v,w。问你v和w是否能够到达u,两个人走过的边不能有重复,否则这条边会被压塌。
具体思路:首先对能形成连通块的进行缩点,构成一个个的连通图。然后这样整个图就变成了一个森林,然后再根据染色后的连通块重新建图。
对于每一次的询问,先看这三个点是不是在同一个连通图里面,如果不是在一个连通图里面肯定是非法情况。
然后再就是讨论在同一个连通图里面的情况,通过讨论v和u的关系来表示出所有的情况。
情况1,lca(u,v)=u.
这种时候肯定是满足的,lca(u,v)=lca(u,w)=lca(v,w)=u.

情况2,lca(u,v)=v.
情况3,lca(u,v)!=u&&lca(u,v)!=v.

讨论上述情况就可以了。
AC代码:
#include<bits/stdc++.h>
using namespace std;
# define ll long long
const int maxn =2e5+;
int n,m,k,num,ordofblock,ordoftree;
vector<int>Edge1[maxn];
vector<int>Edge2[maxn];
int dfn[maxn],low[maxn];
int tree[maxn],block[maxn],vis[maxn];
int depth[maxn],father[maxn][];
stack<int>q;
void init()
{
for(int i=; i<=n; i++)
{
Edge1[i].clear();
Edge2[i].clear();
vis[i]=;
dfn[i]=;
low[i]=;
tree[i]=;
block[i]=;
depth[i]=;
for(int j=;j<=;j++)father[i][j]=;
}
while(!q.empty())
q.pop();
num=;
ordofblock=ordoftree=;
}
void tarjan(int cur,int fa,int ord)
{
tree[cur]=ord;
low[cur]=dfn[cur]=++num;
q.push(cur);
int flag=;
for(int i=; i<Edge1[cur].size(); i++)
{
int to=Edge1[cur][i];
if(to==fa)
{
if(++flag<)
continue;
}
if(!dfn[to])
{
tarjan(to,cur,ord);
low[cur]=min(low[cur],low[to]);
}
else if(!block[to])
{
low[cur]=min(low[cur],dfn[to]);
}
}
if(low[cur]==dfn[cur])
{
ordofblock++;
int tmp;
do
{
tmp=q.top();
q.pop();
block[tmp]=ordofblock;
}
while(tmp!=cur);
}
}
void dfs(int u,int root)
{
vis[u]=;
depth[u]=depth[root]+;
father[u][]=root;
for(int i=; (<<i)<=depth[u]; i++)
{
father[u][i]=father[father[u][i-]][i-];
}
for(int i=; i<Edge2[u].size(); i++)
{
int to=Edge2[u][i];
if(to==root)
continue;
dfs(to,u);
}
}
int lca(int t1,int t2)
{
if(depth[t1]>depth[t2])
swap(t1,t2);
for(int i=; i>=; i--)
{
if(depth[t1]<=depth[t2]-(<<i))
{
t2=father[t2][i];
}
}
if(t1==t2)
return t1;
for(int i=; i>=; i--)
{
if(father[t1][i]!=father[t2][i])
{
t1=father[t1][i];
t2=father[t2][i];
}
}
return father[t1][];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&k);
init();
int st,ed;
for(int i=; i<=m; i++)
{
scanf("%d %d",&st,&ed);
Edge1[st].push_back(ed);
Edge1[ed].push_back(st);
}
for(int i=; i<=n; i++)
{
if(!dfn[i])
{
ordoftree++;
tarjan(i,,ordoftree);
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<Edge1[i].size(); j++)
{
int to=Edge1[i][j];
if(block[i]!=block[to])
{
Edge2[block[i]].push_back(block[to]);
}
}
}
for(int i=; i<=ordofblock; i++)
{
if(!vis[i])
dfs(i,i);
}
int u,v,w;
for(int i=; i<=k; i++)
{
scanf("%d %d %d",&u,&v,&w);
if(tree[u]!=tree[v]||tree[u]!=tree[w]||tree[v]!=tree[w])
{
printf("No\n");
continue;
}
int flag=;
u=block[u],v=block[v],w=block[w];
int uv=lca(u,v);
int uw=lca(u,w);
int vw=lca(v,w);
if(uv==u){
if(uw==u&&vw==u)flag=;
else if(uw!=u)flag=;
else flag=;
}
else if(uv==v){
if(uw==u)flag=;
else flag=;
}
else {
if(uw!=u)flag=;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
return ;
}
H - Rescue the Princess ZOJ - 4097 (tarjan缩点+倍增lca)的更多相关文章
- Day10 - A - Rescue the Princess ZOJ - 4097
Princess Cjb is caught by Heltion again! Her knights Little Sub and Little Potato are going to Helti ...
- hdu 4674 Trip Advisor(缩点+倍增lca)
花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连 ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP
1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 787 Solved: 318[Submit][Stat ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- BZOJ 1051 受欢迎的牛(Tarjan缩点)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4573 Solved: 2428 [Submit][S ...
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
随机推荐
- mybatis的一种批量更新方法【我】
接手一个项目,项目主要架构用的 servlet 3.0 + spring + mybatis 其中发现一个问题: 操作数据时,批量插入可以,批量更新,使用各种写法都无法成功,直接报 mybatis转换 ...
- poj 2566"Bound Found"(尺取法)
传送门 参考资料: [1]:http://www.voidcn.com/article/p-huucvank-dv.html 题意: 题意就是找一个连续的子区间,使它的和的绝对值最接近target. ...
- iis8.0 https配置教程
打开iis>选择左侧根>点击右侧服务器证书 打开界面后 空白处点击右键选择导入 成功导入证书 选择需要绑定证书的网站点击选择>编辑绑定>ssl证书请选择您导入的证书 点击SSL ...
- hystrix实战
https://blog.csdn.net/Ezreal_King/article/details/72942823
- phpcms 标签含义大全
{template "content","header"} ---------- 调用根目录下phpcms\template\content\header ...
- 这3周以来的面试总结(C#/.net 智能硬件/物联网)
2017.3找工作面试记录-第一周 2017.3找工作面试记录-第一周(2) 2017.3找工作面试记录-第二周 2017.4找工作面试记录-第三周 2017.4找工作面试记录-第三周(2)--金蝶 ...
- 使用WinPcap(SharpPcap)实现ARP抓包以实现设备IP修改功能
参考上一篇: 使用WinPcap(SharpPcap)实现ARP抓包以实现设备IP搜索功能 搜索出设备后,需要修改设备IP网关掩码等信息 继续采用ARP包 getBas类似之前的,根据用户电脑的网卡物 ...
- linux报错汇总
一.出现cannot send message: Process exited with a non-zero status错误 查看log文件:sudo cat /var/log/mail.err, ...
- spring创建bean模式singleton与prototype的区别
spring 创建bean有单例模式(singleton)和原始模型模式(prototype)这两种模式. 在默认的情况下,Spring中创建的bean都是单例模式的(注意Spring的单例模式与Go ...
- Hudson管理平台自动化部署war包
继上一篇:Hudson持续集成管理平台搭建 后,我们需要进行项目的自动化部署. 首先:我们先配置<系统管理>中的[系统设置] 然后返回到首页,点击项目名称进入项目内,点击设置: 翻到最底下 ...