ZOJ 4097 Rescue the Princess
在这个物欲横流的社会
oj冷漠无情
只有这xx还有些温度
越界就越界吧 wrong 怎么回事。。。。
给出一个图
然后给出q次询问
问是否存在v和w分别到u的路径且边不重复
在边双连通分量中 任意两点 都至少存在两条边不重复的路径
那么若u v w 在同一连通图中
则存在
若不在
则只有u在 v 和 w 中间时才存在
想一想是不是
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, maxm = , maxh = ;
int n, m, q;
int head[maxn], nex[maxm], cnt, head2[maxn], cnt2;
int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
int pa[maxn], anc[maxn][maxh + ], deep[maxn], instack[maxn];
int f[maxn];
stack<int> s; struct node
{
int u, v;
}Node[maxm]; void add_(int u, int v)
{
Node[cnt].u = u;
Node[cnt].v = v;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v)
{
add_(u, v);
add_(v, u);
} struct edge{
int u, v, next;
}Edge[maxm]; void add2(int u, int v)
{
Edge[cnt2].u = u;
Edge[cnt2].v = v;
Edge[cnt2].next = head2[u];
head2[u] = cnt2++;
} void tarjan(int u,int fa){
low[u] = pre[u] = ++dfs_clock;
s.push(u);
instack[u] = ;
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(i == (fa ^ )) continue;
if(!pre[v])
{
tarjan(v, i);
low[u] = min(low[u], low[v]);
}
else if(instack[v])
low[u] = min(low[u], pre[v]); }
if(pre[u] == low[u])
{
scc_cnt++;
for(;;)
{
int x = s.top(); s.pop();
sccno[x] = scc_cnt;
instack[x] = ;
if(x == u) break;
}
}
} int dfs(int u, int fa)
{
for(int i = ; i < maxh; i++)
anc[u][i] = anc[anc[u][i - ]][i - ];
for(int i = head2[u]; i != -; i = Edge[i].next)
{
int v = Edge[i].v;
if(v == fa || deep[v]) continue;
anc[v][] = u;
deep[v] = deep[u] + ;
dfs(v, u);
}
} int lca(int u, int v)
{
if(deep[u] < deep[v]) swap(u, v);
for(int i = maxh; i >= ; i--)
if(deep[anc[u][i]] >= deep[v])
u = anc[u][i]; for(int i = maxh; i >= ; i--)
{
if(anc[u][i] != anc[v][i])
{
u = anc[u][i];
v = anc[v][i];
}
}
if(u == v) return u;
return anc[u][];
} int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} void init()
{
rep(i, , maxn) f[i] = i;
mem(head, -);
mem(head2, -);
mem(deep, );
dfs_clock = scc_cnt = cnt = cnt2 = ;
mem(sccno, ); mem(pre, );
mem(low, );
mem(anc, );
mem(instack, );
} int main()
{
int T;
rd(T);
while(T--)
{
init();
rd(n), rd(m), rd(q);
rap(i, , m)
{
int u, v;
rd(u), rd(v);
if(u == v) continue;
add(u, v);
}
for(int i = ; i <= n; i++) if(!pre[i]) tarjan(i, -);
rap(u, , n)
{
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(sccno[u] != sccno[v])
{
add2(sccno[u], sccno[v]);
int l = find(sccno[u]), r = find(sccno[v]);
if(l == r) continue;
f[l] = r;
}
}
}
rap(i, , scc_cnt)
if(!deep[i]) deep[i] = , dfs(i, -);
rap(i, , q)
{
int u, v, w;
rd(u), rd(v), rd(w);
if(find(sccno[u]) != find(sccno[v]) || find(sccno[u]) != find(sccno[w]))
{
puts("No");
continue;
}
if(sccno[v] != sccno[w])
{
int lc = lca(sccno[v], sccno[w]);
int ans1 = lca(sccno[u], sccno[v]);
int ans2 = lca(sccno[u], sccno[w]);
int ans3 =lca(lc, sccno[u]);
if(ans3 == lc && (ans1 == sccno[u] || ans2 == sccno[u]))
{
puts("Yes");
}
else
{
puts("No");
}
}
else
{
if(sccno[u] == sccno[v] )
{
puts("Yes");
continue;
}
else
puts("No");
}
}
} return ;
}
ZOJ 4097 Rescue the Princess的更多相关文章
- ZOJ 4097 Rescue the Princess 边双缩点+LCA
给你一个图和三个点U,V,W 问你是否存在从U到V和从U到W的两条边不相交路径 先边双缩点 再每个连通分量搞LCA 最后LCA判 #include<bits/stdc++.h> usin ...
- H - Rescue the Princess ZOJ - 4097 (tarjan缩点+倍增lca)
题目链接: H - Rescue the Princess ZOJ - 4097 学习链接: zoj4097 Rescue the Princess无向图缩点有重边+lca - lhc..._博客园 ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- 山东省第四届acm.Rescue The Princess(数学推导)
Rescue The Princess Time Limit: 1 Sec Memory Limit: 128 MB Submit: 412 Solved: 168 [Submit][Status ...
- 计算几何 2013年山东省赛 A Rescue The Princess
题目传送门 /* 已知一向量为(x , y) 则将它旋转θ后的坐标为(x*cosθ- y * sinθ , y*cosθ + x * sinθ) 应用到本题,x变为(xb - xa), y变为(yb ...
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
- SDUT 2603:Rescue The Princess
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- 2013山东省“浪潮杯”省赛 A.Rescue The Princess
A.Rescue The PrincessDescription Several days ago, a beast caught a beautiful princess and the princ ...
- 山东省赛A题:Rescue The Princess
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3230 Description Several days ago, a beast caught ...
随机推荐
- Easyui datagrid 修改分页组件的分页提示信息为中文
datagrid 修改分页组件的分页提示信息为中文 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 问题描述 默认分页组件为英文展示,如下,希望改成中文展示 ...
- Android学习笔记之SoftReference软引用,弱引用WeakReference
SoftReference可以用于bitmap缓存 WeakReference 可以用于handler 非静态内部类和匿名内部类容易造成内存泄漏 private Handler mRemoteHand ...
- [伟哥开源项目基金会](https://github.com/AspNetCoreFoundation)
伟哥开源项目基金会 GitHub_base=> 伟哥开源项目基金会 该项目作者为伟哥,GitHub地址:https://github.com/amh1979: 该项目维护者为鸟窝,GitHub地 ...
- RHEL 6.6下Python 2.6.6升级到Python 3.6.6
最近一段时间shell脚本写得很溜,很有成就感,一想到被自己落下的Python就感到十分心虚.开始坚持学习Python!先将自己的测试机器的Python升级到Python 3.6.6.简单整理.记 ...
- 前后端分离djangorestframework—— 接入支付宝支付平台
支付宝 简介 支付宝是什么不用多说了,本次教程适合初学者 前提准备 话不多说,干就完了 1.注册开发者账号,设置公钥私钥 首先进入支付宝开发者平台:传送门 ,有账号直接登录,没账号用你平时用来付款收钱 ...
- linux缺页异常处理--内核空间
缺页异常被触发通常有两种情况-- 程序设计的不当导致访问了非法的地址 访问的地址是合法的,但是该地址还未分配物理页框. 下面解释一下第二种情况,这是虚拟内存管理的一个特性.尽管每个进程独立拥有3GB的 ...
- CFS调度器(1)-基本原理
首先需要思考的问题是:什么是调度器(scheduler)?调度器的作用是什么?调度器是一个操作系统的核心部分.可以比作是CPU时间的管理员.调度器主要负责选择某些就绪的进程来执行.不同的调度器根据不同 ...
- Jenkins自动化部署-----持续交付【转】
感谢之前带领过我的leader,让我能够知道什么是好的开发方法. 在很早之前就接触过敏捷开发.什么是敏捷开发,简单来说就是让软件可靠地,快速地发布出来的一种开发方法和技巧. 而敏捷开发中有许多的实践, ...
- Linux(Centos7)下搭建SVN服务器 (转载)
系统环境:centos7.2 第一步:通过yum命令安装svnserve,命令如下: yum -y install subversion 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停 ...
- 我的ElasticSearch之ElasticSearch安装配置环境
最近一段时间比较忙,都很少来园子逛了,刚好,用到了ElasticSearch,感觉还不错,所以就给大家推荐一下,自己也顺便学习:虽然公司选择用ElasticSearch,但是以前都没有用过这个,而且公 ...