BZOJ1997 [Hnoi2010]Planar 【2-sat】
题目链接
题解
显然相交的两条边不能同时在圆的一侧,\(2-sat\)判一下就好了
但这样边数是\(O(m^2)\)的,无法通过此题
但是\(n\)很小,平面图 边数上界为\(3n - 6\),所以过大的\(m\)可以判掉
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 10005,maxm = 8000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
int n,m,N,pos[maxn],a[maxn],b[maxn],vis[maxn];
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,cnt,top;
inline void build(int u,int v){ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;}
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
to = ed[k].to;
if (!dfn[to]){
dfs(to);
low[u] = min(low[u],low[to]);
}
else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do {
Scc[st[top]] = scci;
}while (st[top--] != u);
}
}
int main(){
int T = read();
while (T--){
n = read(); m = read();
REP(i,m) a[i] = read(),b[i] = read(),vis[i] = false;
REP(i,n) pos[read()] = i;
if (m > 3 * n - 6) {puts("NO"); continue;}
REP(i,m){
a[i] = pos[a[i]]; b[i] = pos[b[i]];
if (a[i] > b[i]) swap(a[i],b[i]);
if (a[i] + 1 == b[i]) vis[i] = true;
}
int tmp = m; m = 0;
REP(i,tmp) if (!vis[i]){
m++;
a[m] = a[i]; b[m] = b[i];
}
N = (m << 1); REP(i,N) h[i] = 0; ne = 0;
int x,y,xx,yy;
for (int i = 1; i <= m; i++){
x = a[i]; y = b[i];
for (int j = i + 1; j <= m; j++){
xx = a[j]; yy = b[j];
if ((x < xx && xx < y && yy > y) || (x < yy && yy < y && xx < x)){
build(i,j + m); build(j + m,i);
}
}
}
cnt = scci = top = 0;
REP(i,N) dfn[i] = low[i] = Scc[i] = 0;
REP(i,N) if (!dfn[i]) dfs(i);
int flag = true;
REP(i,m) if (Scc[i] == Scc[i + m]){
flag = false; break;
}
puts(flag ? "YES" : "NO");
}
return 0;
}
BZOJ1997 [Hnoi2010]Planar 【2-sat】的更多相关文章
- [bzoj1997][Hnoi2010]Planar(2-sat||括号序列)
开始填连通分量的大坑了= = 然后平面图有个性质m<=3*n-6..... 由平面图的欧拉定理n-m+r=2(r为平面图的面的个数),在极大平面图的情况可以代入得到m=3*n-6. 网上的证明( ...
- bzoj千题计划231:bzoj1997: [Hnoi2010]Planar
http://www.lydsy.com/JudgeOnline/problem.php?id=1997 如果两条边在环内相交,那么一定也在环外相交 所以环内相交的两条边,必须一条在环内,一条在环外 ...
- [BZOJ1997][Hnoi2010]Planar 2-sat (联通分量) 平面图
1997: [Hnoi2010]Planar Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2317 Solved: 850[Submit][Stat ...
- bzoj 1997: [Hnoi2010]Planar【瞎搞+黑白染色】
脑补一下给出的图:一个环,然后有若干连接环点的边,我们就是要求这些边不重叠 考虑一下不重叠的情况,两个有交边一定要一个在环内一个在环外,所以把相交的边连边,然后跑黑白染色看是否能不矛盾即可(可能算个2 ...
- BZOJ1997 [Hnoi2010]Planar (2-sat)
题意:给你一个哈密顿图,判断是不是平面图 思路:先找出哈密顿图来.哈密顿回路可以看成一个环,把边集划分成两个集合,一个在环内,一个在外.如果有两条相交边在环内,则一定不是平面图,所以默认两条相交边,转 ...
- bzoj1997: [Hnoi2010]Planar
2-SAT. 首先有平面图定理 m<=3*n-6,如果不满足这条件肯定不是平面图,直接退出. 然后构成哈密顿回路的边直接忽略. 把哈密顿回路当成一个圆, 如果俩条边交叉(用心去感受),只能一条边 ...
- bzoj1997 [Hnoi2010]Planar——2-SAT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1997 神奇的经典2-SAT问题! 对于两个相交的区间,只能一里一外连边,所以可以进行2-SA ...
- 【BZOJ1997】[Hnoi2010]Planar 2-SAT
[BZOJ1997][Hnoi2010]Planar Description Input Output Sample Input 2 6 9 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 ...
- 【刷题】BZOJ 2001 [Hnoi2010]City 城市建设
Description PS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少 ...
随机推荐
- Pandas v0.23.4手册汉化
Pandas手册汉化 此页面概述了所有公共pandas对象,函数和方法.pandas.*命名空间中公开的所有类和函数都是公共的. 一些子包是公共的,其中包括pandas.errors, pandas. ...
- 用树莓派Raspberry Pi和Micro:bit做一个自拍器
在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...
- Mybatis利用拦截器做统一分页
mybatis利用拦截器做统一分页 查询传递Page参数,或者传递继承Page的对象参数.拦截器查询记录之后,通过改造查询sql获取总记录数.赋值Page对象,返回. 示例项目:https://git ...
- openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一
openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...
- STC 单片机ADC实现原理
模数转换器原理 数模转换器( analog to digitI converter,ADC),简称为A/D,ADC是链接模拟世界和数字世界的桥梁.它用于将连续的模拟信号转换为数字形式离散信号.典型的, ...
- kaldi DNN在线解码 aishell为例
在kaldi 的工具集里有好几个程序可以用于在线识别.这些程序都位在src/onlinebin文件夹里,他们是由src/online文件夹里的文件编译而成(你现在可以用make ext 命令进行编译) ...
- Hyperledger Fabric中的Identity
Hyperledger Fabric中的Identity 什么是Identity 区块链网络中存在如下的角色:peers, orderers, client application, administ ...
- 基于 Agent 的模型入门:Python 实现隔离仿真
2005 年诺贝尔经济学奖得主托马斯·谢林(Thomas Schelling)在上世纪 70 年代就纽约的人种居住分布得出了著名的 Schelling segregation model,这是一个 A ...
- python实现中文验证码识别方法(亲测通过)
验证码截图如下: # coding:utf-8from PIL import Image,ImageEnhanceimport pytesseract#上面都是导包,只需要下面这一行就能实现图片文字识 ...
- LeetCode 48. Rotate Image (C++)
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...