[BZOJ1997][Hnoi2010]Planar 2-sat (联通分量) 平面图
1997: [Hnoi2010]Planar
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 2317 Solved: 850
[Submit][Status][Discuss]
Description

Input

Output

Sample Input
6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
1 4 2 5 3 6
5 5
1 2
2 3
3 4
4 5
5 1
1 2 3 4 5
Sample Output
YES
HINT
Source
太强辣。
平面图的边数不大于3*n-6剪枝。
先将环提出来,对于剩下的边,我们可以选择在环外连还是在环内连。
对于不相交的边,显然在环外连和在环内连都不会相交。
对于相交的边,显然不能同时在环外连或在环内连。只能一个在环外一个在环内。
将一条边拆为环外、环内两条边,构造2-sat。
判断2*i和2*i-1是否在同一个联通分量里。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define LL long long
#define mod 19650827
using namespace std;
int read() {
char ch=getchar();int x=,f=;
while(!isdigit(ch)){ch=getchar();}
while(isdigit(ch)){x=x*+ch-'';ch=getchar();}
return x;
}
int T;
int n,m;
struct data {
int u,v;
}a[];
int pos[];
int cnt=;
int head[],cntt;
struct edge {
int to,next;
}e[];
void add(int u,int v) {e[cntt].to=v;e[cntt].next=head[u];head[u]=cntt++;}
int dfn[],low[],inq[],sz;
int sta[],top,bl[],scc;
void tarjan(int now) {
dfn[now]=low[now]=++sz;
sta[++top]=now;inq[now]=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(!dfn[to]) {tarjan(to);low[now]=min(low[now],low[to]);}
else if(inq[to]) {low[now]=min(low[now],dfn[to]);}
}
if(low[now]==dfn[now]) {
int x=-;
scc++;
while(x!=now) {
x=sta[top--];inq[x]=;
bl[x]=scc;
}
}
}
int main() {
T=read();
while(T--) {
cnt=;cntt=;top=;scc=;sz=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));memset(low,,sizeof(low));
memset(inq,,sizeof(inq));
n=read(),m=read();
for(int i=;i<=m;i++) a[i].u=read(),a[i].v=read();
for(int i=;i<=n;i++) pos[read()]=i;
if(m>*n-){printf("NO\n");continue;}
for(int i=;i<=m;i++) {
int c=abs(pos[a[i].u]-pos[a[i].v]);
if(c==||(max(pos[a[i].u],pos[a[i].v])==n&&min(pos[a[i].u],pos[a[i].v])==)) continue;
a[++cnt]=a[i];
}
for(int i=;i<=cnt;i++) {
for(int j=i+;j<=cnt;j++) {
int t1=pos[a[i].u],t2=pos[a[i].v];
if(t1>t2) swap(t1,t2);
int t3=pos[a[j].u],t4=pos[a[j].v];
if(t3>t4) swap(t3,t4);
if((t1<t3&&t2<t4&&t2>t3)||(t1>t3&&t2>t4&&t1<t4)) {
add(*i-,*j);add(*j,*i-);
add(*i,*j-);add(*j-,*i);
}
}
}
for(int i=;i<=*cnt;i++) if(!dfn[i]) tarjan(i);
bool flag=;
for(int i=;i<=cnt;i++) if(bl[*i]==bl[*i-]){printf("NO\n");flag=;break;}
if(flag)printf("YES\n");
}
}
[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)
题意:给你一个哈密顿图,判断是不是平面图 思路:先找出哈密顿图来.哈密顿回路可以看成一个环,把边集划分成两个集合,一个在环内,一个在外.如果有两条相交边在环内,则一定不是平面图,所以默认两条相交边,转 ...
- BZOJ1997 [Hnoi2010]Planar 【2-sat】
题目链接 BZOJ1997 题解 显然相交的两条边不能同时在圆的一侧,\(2-sat\)判一下就好了 但这样边数是\(O(m^2)\)的,无法通过此题 但是\(n\)很小,平面图 边数上界为\(3n ...
- 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]平面图判定Plana
bzoj1997 [HNOI2010]平面图判定Planar 链接 bzoj luogu 思路 好像有很多种方法过去.我只说2-sat 环上的边,要不在里面,要不在外边. 有的边是不能同时在里面的,可 ...
- 【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 1997: [Hnoi2010]Planar( 2sat )
平面图中E ≤ V*2-6.. 一个圈上2个点的边可以是在外或者内, 经典的2sat问题.. ----------------------------------------------------- ...
随机推荐
- 网易OpenStack部署运维实战
OpenStack自2010年项目成立以来,已经有超过200个公司加入了 OpenStack 项目,目前参与 OpenStack 项目的开发人员有 17,000+,而且这些数字还在增加,作为一个开源的 ...
- 【Pascal's Triangle】cpp
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- 如何创造财富?硅谷创业之父 Paul Graham 《黑客与画家》思维导图
先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 下载大图:http://caifujianghu.com/article/ruhe-c ...
- ASP.NET Core [2]:Middleware-请求管道的构成(笔记)
原文链接:http://www.cnblogs.com/RainingNight/p/middleware-in-asp-net-core.html 中间件处理请求主要分为三个阶段:1. 中间件的注册 ...
- ironic baremetal node rescue/unrescue mode
环境ironic-api ironic-conductor,ironicclient均升级为Queens版本 官网说明API版本为1.38才支持rescue/unrescue,所以修改下openrc文 ...
- juqery基本选择器和层级选择器
1.基本选择器,主要通过标签名称,样式,id等选择标签,如下代码是简单的使用 (1)根据标签或者样式筛选 <!DOCTYPE html> <html lang="en&qu ...
- Learn the shell
learn the shell what is the shell? when we speak of the command line,we are really to the shell.Actu ...
- linux常见的编码转换
1.如何界定是utf-8编码还是其他如 ANSI 或者gb2312编码 以“浙”这个汉字为例,用16进制编码查看时,显示 D5 E3 为2个字节,则为 ansi或者gb2312编码 "苏&q ...
- foreach的理解
foreach的两种写法的解读 一.常见 1.理解:将数组元素逐个赋值给变量V,然后将v输出 2.代码: $arr = array(1,2,3,4,5); foreach($arr as $a){ e ...
- 存储 磁盘大于2TB 大数据存储一个盘 解决方法
1.vmware虚拟机环境下可以做裸映射 但是一个存储 只能对应一个虚拟主机裸映射 我已经在一个10TB的存储上做好多个主机,就不适用了 2.在虚拟机上添加5个2TB磁盘,磁盘管理中新建 带区卷 可以 ...