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 ...
随机推荐
- 交叉编译jpeglib遇到的问题
由于要在开发板中加载libjpeg,不能使用gcc编译的库文件给以使用,需要自己配置使用另外的编译器编译该库文件. /usr/bin/ld: .libs/jaricom.o: Relocations ...
- Unity 光照着色器
光照着色器需要考虑光照的分类,一般分为漫反射和镜面反射. 漫反射计算基本光照: float brightness=dot(normal,lightDir) 将法线和光的入射方向进行点积运算,求出 ...
- OpenCV编译以及QT Creator配置
OpenCV编译以及QT Creator配置 在进行编译前,需下载以下工具和源码: CMake ---- 用于编译: 下载地址; https://cmake.org/ 安装在D:\Program Fi ...
- 互联网运营+SEO:推荐必看的5本书籍
本文首发于:风云社区(scoee.com) 最近开始学习和研究互联网运营和SEO,对于我这个小白来讲,还是有些吃力,毕竟从来没接触这方面的,尽管在之前的软件公司做过售前和产品相关的工作,但毕竟与互联网 ...
- django_orm查询性能优化
查询操作和性能优化 1.基本操作 增 models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs obj = mode ...
- Java面试题全集(上)转载
Java面试题全集(上) 2013年年底的时候,我看到了网上流传的一个叫做<Java面试题大全>的东西,认真的阅读了以后发现里面的很多题目是重复且没有价值的题目,还有不少的参考答案也是 ...
- 9.组合模式(Composite Pattern)
动机(Motivate): 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素 ...
- HDU - 5073 Galaxy(数学)
题目 题意:n个点,运行移动k个点到任何位置,允许多个点在同一位置上.求移动k个点后,所有点到整体中心的距离的平方和最小. 分析:这题题目真的有点迷...一开始看不懂.得知最后是选取一个中心,于是看出 ...
- python实现单向循环链表
单向循环链表 单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点. 实现 class Node(object): """节 ...
- Groovy 设计模式 -- Strategy 模式
策略模式 https://en.wikipedia.org/wiki/Strategy_pattern In computer programming, the strategy pattern (a ...