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 ...
随机推荐
- SAP MM ME1M报表结果不科学?
SAP MM ME1M报表结果不科学? 做过SAP MM顾问的都知道,报表ME1M可以查询物料的info record列表,即是说可以以列表的形式批量显示多个物料的采购价格主数据. 但是这个报表有个不 ...
- 在IIS上部署你的ASP.NET Core 2.1项目
1.在控制面板→程序→启用或关闭Windows功能→勾选Internet Information Services以及Web管理工具下的IIS管理控制台 2.IIS 安装AspNetCoreModul ...
- Android入门第一课之Java基础
通知:由于本周六场地申请没通过,所以本周的培训临时取消. 今天给大家带来的是Android入门的第一课,由于教室申请的不确定性,因此,每次培训的内容都会在博客先提前释放出来.首先Android的APP ...
- java基础知识总结一:
四种内部类 直接抛出异常 单例模式: 懒汉式单例.饿汉式单例.登记式单例 []关于内部类: []关于异常: 直接捕捉并抛出异常:不需要给异常添加名字: if(i>10)throw ...
- swiper 自定义分页器的使用
网上关于swiper 自定义分页器的方法比较多,但是已经不适合使用.它的API又比较坑爹,什么都是点到为止,不说清楚.因为要做一个产品颜色切换的效果,有黑与白两种颜色,因此尝试使用Swiper的自定义 ...
- Kubernetes Dashboard - 每天5分钟玩转 Docker 容器技术(173)
前面章节 Kubernetes 所有的操作我们都是通过命令行工具 kubectl 完成的.为了提供更丰富的用户体验,Kubernetes 还开发了一个基于 Web 的 Dashboard,用户可以用 ...
- C#常用的网络组件
常用的网络组件 using System.Net;//为多种网络协议提供统一和简单的编程接口 using System.Net.Mail;//为简单邮件传输协议的服务器提供E-mail发送的类 usi ...
- JavaScript(三)数据类型转换
类型转换JavaScript中的取值类型非常灵活,如当JavaScript期望使用一个布尔值的时候,你可以提供其它数据类型的,JavaScript将根据需要自行转换数据类型.如下示例: 10 + “o ...
- 【转载】xilinx 高速收发器Serdes深入研究
此篇文章深入浅出介绍了关于高速串行收发器的几个重要概念和注意事项,为方便知识点复习总结和后续查阅特此转载,原文标题及链接为:xilinx 高速收发器Serdes深入研究 - CSDN博客 http ...
- SAP CRM 集类型(Set Type)与产品层次(Product Hierarchy)
本文是产品与对象相关的部分SAP文档的翻译,不包含配置部分. 本文链接:https://www.cnblogs.com/hhelibeb/p/10112723.html 1,对象(Objects) 对 ...