[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问题.. ----------------------------------------------------- ...
随机推荐
- Hadoop学习笔记系列
Hadoop学习笔记系列 一.为何要学习Hadoop? 这是一个信息爆炸的时代.经过数十年的积累,很多企业都聚集了大量的数据.这些数据也是企业的核心财富之一,怎样从累积的数据里寻找价值,变废为宝炼 ...
- Start with PJSIP on windows
To overcome the project of HD video conferencing systerm,I should learn to use the PJSIP. I should m ...
- C# 6.0/7.0 的新特性
转眼C#语言都已经迭代到7.0版本了,很多小伙伴都已经把C# 7.0 的新特性应用到代码中了,想想自己连6.0的新特性都还很少使用,今天特意搜集了一下6.0和7.0的一些新特性,记录一下,方便查阅. ...
- Python 3基础教程10-全局变量和局部变量
本文来讲讲全局变量和局部变量,前面学习了函数的基本使用,所以,这里就要注意变量的使用和访问权限. 试试下面的demo.py
- 【Linux】wc :字数统计命令
wc :(Word Count) 统计每个传入文件中行数.词数与字节数 $ wc py_this # 三个数字分别对应行数.词数和字节数 21 144 857 py_this $ wc py_this ...
- php 根据文件内容来判断文件类型
/*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6 ...
- 设置EntityFramework中decimal类型数据精度问题(EF默认将只会保留到2为精度)
原文:设置EntityFramework中decimal类型数据精度 EF中默认的decimal数据精度为两位数,当我们数据库设置的精度大于2时,EF将只会保留到2为精度. e.g. .19990将会 ...
- [CTSC2017][bzoj4903] 吉夫特 [状压dp+Lucas定理]
题面 传送门 思路 一句话题意: 给出一个长度为 n 的序列,求所有长度大于等于2的子序列个数,满足:对于子序列中任意两个相邻的数 a和 b (b 在 a 前面),$C_a^b mod 2=1$,答案 ...
- Vitamio 视频播放
资料总结 Vitamio官网:https://www.vitamio.org 源码地址:https://github.com/yixia/VitamioBundle 最佳教程:大名鼎鼎的农民伯伯博客 ...
- Fiddler抓取HTTP请求
参考链接:http://blog.csdn.net/ohmygirl/article/details/17849983/ http://www.cnblogs.com/kingwolf_JavaScr ...