题意:给你一个哈密顿图,判断是不是平面图

思路:先找出哈密顿图来。哈密顿回路可以看成一个环,把边集划分成两个集合,一个在环内,一个在外。如果有两条相交边在环内,则一定不是平面图,所以默认两条相交边,转化成2——sat,两条边不能同时在内或外,注意双向加边。(以边来转化成两倍)

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
const int maxn = + ;
int r(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int T,n,m,ind,top,cnt,scc;
int u[],v[];
int c[],pos[];
int last[],dfn[],low[],q[],bl[];
bool inq[]; struct edge{
int to,next;
}e[]; void add(int u,int v){
e[++cnt].to=v;
e[cnt].next=last[u];
last[u]=cnt;
} void tarjan(int x){
inq[x]=;q[++top]=x;
low[x]=dfn[x]=++ind;
for(int i=last[x];i;i=e[i].next)
if(!dfn[e[i].to])
tarjan(e[i].to),low[x]=min(low[x],low[e[i].to]);
else if(inq[e[i].to])
low[x]=min(low[x],dfn[e[i].to]);
int now=-;
if(low[x]==dfn[x])
{
scc++;
while(now!=x)
{
now=q[top--];inq[now]=;
bl[now]=scc;
}
}
} bool jude(){
for(int i=;i<=m;i++){
if(bl[i*]==bl[i*-])
return false;
}
return true;
}
int main(){
T=r();
while(T--){
n=r(),m=r();
for(int i=;i<=m;i++){
u[i]=r();v[i]=r();
}
clc(last,);
cnt=;
scc=ind=;
clc(low,);
clc(dfn,);
for(int i=;i<=n;i++)
c[i]=r();
if(m>*n-){
printf("NO\n");
continue;
}
for(int i=;i<=n;i++)
pos[c[i]]=i;
top=;
for(int i=;i<=m;i++){
u[i]=pos[u[i]],v[i]=pos[v[i]];
if(u[i]>v[i]) swap(u[i],v[i]);
if(v[i]-u[i]==||v[i]-u[i]==n-) continue;
u[++top]=u[i],v[top]=v[i];
}
m=top;
for(int i=;i<=m;i++){
for(int j=i+;j<=m;j++){
if((u[i]<u[j]&&v[i]>u[j]&&v[i]<v[j])||(u[i]>u[j]&&v[j]>u[i]&&v[i]>v[j])){
add(*i-,*j);
add(*i,*j-);
add(*j-,*i);
add(*j,*i-);
}
}
}
for(int i=;i<=*m;i++){
if(dfn[i]==){
tarjan(i);
}
}
if(jude()) printf("YES\n");
else printf("NO\n"); }
return ;
}

BZOJ1997 [Hnoi2010]Planar (2-sat)的更多相关文章

  1. [bzoj1997][Hnoi2010]Planar(2-sat||括号序列)

    开始填连通分量的大坑了= = 然后平面图有个性质m<=3*n-6..... 由平面图的欧拉定理n-m+r=2(r为平面图的面的个数),在极大平面图的情况可以代入得到m=3*n-6. 网上的证明( ...

  2. bzoj千题计划231:bzoj1997: [Hnoi2010]Planar

    http://www.lydsy.com/JudgeOnline/problem.php?id=1997 如果两条边在环内相交,那么一定也在环外相交 所以环内相交的两条边,必须一条在环内,一条在环外 ...

  3. [BZOJ1997][Hnoi2010]Planar 2-sat (联通分量) 平面图

    1997: [Hnoi2010]Planar Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2317  Solved: 850[Submit][Stat ...

  4. BZOJ1997 [Hnoi2010]Planar 【2-sat】

    题目链接 BZOJ1997 题解 显然相交的两条边不能同时在圆的一侧,\(2-sat\)判一下就好了 但这样边数是\(O(m^2)\)的,无法通过此题 但是\(n\)很小,平面图 边数上界为\(3n ...

  5. bzoj1997: [Hnoi2010]Planar

    2-SAT. 首先有平面图定理 m<=3*n-6,如果不满足这条件肯定不是平面图,直接退出. 然后构成哈密顿回路的边直接忽略. 把哈密顿回路当成一个圆, 如果俩条边交叉(用心去感受),只能一条边 ...

  6. bzoj1997 [Hnoi2010]Planar——2-SAT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1997 神奇的经典2-SAT问题! 对于两个相交的区间,只能一里一外连边,所以可以进行2-SA ...

  7. 【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 ...

  8. bzoj1997 [HNOI2010]平面图判定Plana

    bzoj1997 [HNOI2010]平面图判定Planar 链接 bzoj luogu 思路 好像有很多种方法过去.我只说2-sat 环上的边,要不在里面,要不在外边. 有的边是不能同时在里面的,可 ...

  9. BZOJ 1997: [Hnoi2010]Planar( 2sat )

    平面图中E ≤ V*2-6.. 一个圈上2个点的边可以是在外或者内, 经典的2sat问题.. ----------------------------------------------------- ...

随机推荐

  1. [Linux]查看本机IP

    命令: ~$ ip addr showor~$ ipconfig 

  2. C# double float int string 与 byte数组 相互转化

    在做通信编程的时候,数据发送多采用串行发送方法,实际处理的时候多是以字节为单位进行处理的.在C/C++中 多字节变量与Byte进行转化时候比较方便 采用UNION即可废话少说看示例:typedef u ...

  3. building Utils {{ant+ivy}、{maven}}怎么样手动将下载下来的 JAR 包添加到 Maven、ivy 的本地仓库

    mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面 ...

  4. Python设计模式——装饰模式(Decorator)

    假如我们需要开发一个程序来展示一个人穿衣服的过程. #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Person(): def __in ...

  5. Java 中正确使用 hashCode 和 equals 方法

    在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...

  6. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  7. ASP.Net 添加 Interop for Word, excel 插件

    1:在服务器上安装office的Excel软件. 2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务" 3:依次双 ...

  8. GridView 根据要求显示指定值

    最近在写一个小项目用来练手恢复一下功力的,在Users表中有一个用户字段是状态,我使用"0"表示启用,“1”表示禁用, 存到数据库中, 由于之前有一段时间没写代码了,所以有点生疏了 ...

  9. 第十章Composite设备

    10.1 Composite设备介绍 USB的Composite类是USB 复合设备类,一个USB设备具有多种设备功能,比如一个USB设备同时具有鼠标和键盘功能.单一的USB设备开发相对简单,但在很多 ...

  10. android usb host 读写USB设备

    自android3.1以后android增加了操作USB设备的API. 官网地址:http://developer.android.com/guide/topics/connectivity/usb/ ...