BZOJ1997:[HNOI2010]PLANAR——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1997
https://www.luogu.org/problemnew/show/P3209
若能将无向图G=(V,E)画在平面上使得任意两条无重合顶点的边不相交,则称G是平面图。判定一个图是否为平面图的问题是图论中的一个重要问题。现在假设你要判定的是一类特殊的图,图中存在一个包含所有顶点的环,即存在哈密顿回路。
m>3*n+6显然为NO。
有一个想法就是把哈密顿回路当成一个壳,枚举每一条边,再枚举另一条边,很容易通过哈密顿序来判断两边是否相交。
那么此时相交是否输出NO呢?并不是。
(我纠结在这里,后来发现我游戏都白玩了,有一个解绳子的游戏,思考一下就知道可以把壳内的边移到壳外就可以解决矛盾。)
于是分成了两个区域:壳内和壳外。用并查集维护一下就行了。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int N=;
const int M=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int u,v;
}e[M];
int n,m,pos[N],t[N],fa[M*];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
inline bool unionn(int x,int y){
int x1,y1;
if(find(x)==find(y))return ;
x1=find(x),y1=find(y+m);
if(x1!=y1)fa[x1]=y1;
x1=find(x+m),y1=find(y);
if(x1!=y1)fa[x1]=y1;
return ;
}
inline bool pan(int i,int j){
int xi=pos[e[i].u],yi=pos[e[i].v];
int xj=pos[e[j].u],yj=pos[e[j].v];
if(xi>yi)swap(xi,yi);
if(xj>yj)swap(xj,yj);
if(xi>xj)swap(xi,xj),swap(yi,yj);
return xi<xj&&xj<yi&&yi<yj;
}
int main(){
int T=read();
while(T--){
bool ok=;
n=read(),m=read();
for(int i=;i<=m;i++){
e[i].u=read(),e[i].v=read();
}
for(int i=;i<=n;i++)t[i]=read(),pos[t[i]]=i;
if(m>*n+){puts("NO");continue;}
for(int i=;i<=*m;i++)fa[i]=i;
for(int i=;i<=m&&ok;i++){
for(int j=i+;j<=m&&ok;j++){
if(pan(i,j)){
ok=unionn(i,j);
}
}
}
if(!ok)puts("NO");
else puts("YES");
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++
BZOJ1997:[HNOI2010]PLANAR——题解的更多相关文章
- Bzoj 1997 [Hnoi2010]Planar题解
1997: [Hnoi2010]Planar Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2224 Solved: 824[Submit][Stat ...
- [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 ...
- 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)
题意:给你一个哈密顿图,判断是不是平面图 思路:先找出哈密顿图来.哈密顿回路可以看成一个环,把边集划分成两个集合,一个在环内,一个在外.如果有两条相交边在环内,则一定不是平面图,所以默认两条相交边,转 ...
- 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 ...
随机推荐
- 【费元星】crt 无法上传文件,总是显示盾牌表示-完美解决
将如下内容保存到文件中,已.bat 结尾 taskkill /f /im explorer.exeattrib -s -r -h "%userprofile%\AppData\Local\i ...
- 了解和分析iOS Crash
WeTest 导读 北京时间凌晨一点,苹果一年一度的发布会如期而至.新机型的发布又会让适配相关的同学忙上一阵子啦,并且iOS Crash的问题始终伴随着移动开发者.本文将从三个阶段,由浅入深的介绍如何 ...
- Android Test和Logcat
一 测试相关概念 是否有源码 黑盒测试: 测试工具 白盒测试: 对所有的源码特别熟悉 对特定的代码进行测试 都是编程 时间 单元测试(程序员) 模块测试 集成测试 系统测试 回归测试(改bug) 压力 ...
- [CodeForce455A]Boredom
题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...
- C 计算员工工资
#include <stdio.h> int main(int argc, char **argv) { //定义四个变量 g每小时固定的工资 40 固定工作时间 pay工资 hours员 ...
- cronolog:日志分割工具
一. 引言 因为tomcat的catalina.out日志无法按照日期自动创建,因此采用cronnlog分割. 二. 安装与配置 1.安装cronolog: yum install -y cronol ...
- Python replace方法并不改变原字符串
直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...
- github 使用“git commit -m"命令时候出现的一个小问题
git commit -m 使用问题 今天提交文件到github,步骤是: git add abc.py (abc.py是我当前随意写的一个文件名) git commit -m 'add codes ...
- spring boot 中文乱码问题
在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...
- [leetcode-748-Largest Number At Least Twice of Others]
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...