条件转化,2-sat BZOJ 1997
http://www.lydsy.com/JudgeOnline/problem.php?id=1997
1997: [Hnoi2010]Planar
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1810 Solved: 684
[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
思路:(from 出题人):http://www.cnblogs.com/jinkun113/p/4894499.html
我们把哈密顿回路在平面上画成一个圆。仔细观察一下。
好像我的代码因为用了map和edges这些东西,所以跑的速度比别人慢一个log= =,别人都是200ms左右,我是2000ms,人生第一次体验到了map所带来的绝望
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
struct TwoSat{
int n, c, s[maxn * ];
bool mark[maxn << ];
vector<int> G[maxn << ]; void init(int n){
this->n = n;
for (int i = ; i < * n; i++) G[i].clear();
memset(mark, false, sizeof(mark));
} void add_edge(int x, int xval, int y, int yval){
x = x * + xval, y = y * + yval;
G[x ^ ].pb(y), G[y ^ ].pb(x);
} void display(){
for (int i = ; i < * n; i++){
printf("from = %d\n", i);
for (int j = ; j < G[i].size(); j++){
printf("%d ", G[i][j]);
}
cout << endl;
}
} bool dfs(int x){
if (mark[x ^ ]) return false;
if (mark[x]) return true;
mark[x] = true;
s[c++] = x;
for (int i = ; i < G[x].size(); i++){
if (!dfs(G[x][i])) return false;
}
return true;
} bool solve(){
for (int i = ; i < n * ; i += ){
if (!mark[i] && !mark[i + ]){
c = ;
if (!dfs(i)){
while (c) mark[s[--c]] = false;
if (!dfs(i + )) return false;
}
}
}
return true;
} };
TwoSat tar;
int n, m;
map<int, int> id;
map<pair<int, int>, int> Point;
vector<pair<int, int> > edges; bool test(int u1, int v1, int u2, int v2){
if (u2 > u1 && u2 < v1 && v2 > v1) return true;
if (u1 > u2 && u1 < v2 && v1 > v2) return true;
return false;
} int main(){
int T; cin >> T;
while (T--){
scanf("%d%d", &n, &m);
id.clear();
edges.clear();
for (int i = ; i < m; i++){
int u, v; scanf("%d%d", &u, &v);
if (u > v) swap(u, v);
edges.push_back(mk(u, v));
}
for (int i = ; i <= n; i++){
int u; scanf("%d", &u);
id[u] = i;
}
if (m >= n * + ){
puts("NO"); continue;
}
tar.init(m);
for (int i = ; i < m; i++){
int x1 = edges[i].fi, y1 = edges[i].se;
x1 = id[x1], y1 = id[y1];
if (x1 > y1) swap(x1, y1); for (int j = i + ; j < m; j++){
int x2 = edges[j].fi, y2 = edges[j].se;
x2 = id[x2], y2 = id[y2];
if (x2 > y2) swap(x2, y2);
//if (x1 == x2 || x1 == y2 || y1 == x2 || y1 == y2) continue;
if (test(x1, y1, x2, y2)){///假定刚开始都是在里面的
tar.add_edge(i, , j, );
tar.add_edge(i, , j, );
}
}
}
//tar.display();
if (tar.solve()) puts("YES");
else puts("NO");
}
return ;
}
条件转化,2-sat BZOJ 1997的更多相关文章
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- [BZOJ 1997][HNOI2010]Planar(2-SAT)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1997 分析: 考虑每条边是在圈子里面还是圈子外面 所以就变成了2-SAT判定问题了= ...
- BZOJ 1997: [Hnoi2010]Planar( 2sat )
平面图中E ≤ V*2-6.. 一个圈上2个点的边可以是在外或者内, 经典的2sat问题.. ----------------------------------------------------- ...
- bzoj 1997 [Hnoi2010]Planar——2-SAT+平面图的一个定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1997 平面图的一个定理:若边数大于(3*点数-6),则该图不是平面图. 然后就可以2-SAT ...
- Bzoj 1997 [Hnoi2010]Planar题解
1997: [Hnoi2010]Planar Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2224 Solved: 824[Submit][Stat ...
- bzoj 1997: [Hnoi2010]Planar
#include<cstdio> #include<cstring> #include<iostream> #define M 20005 #define N 20 ...
- 【BZOJ 1997】[Hnoi2010]Planar
Description Input Output 找到哈密尔顿环之后找到不在哈密尔顿环上的边 这些边如果同时在里面相交那他们同时在外面也相交,所以只能一外一内,这就变成了2-SAT,判一下就好了 ...
- bzoj 1997: [Hnoi2010]Planar【瞎搞+黑白染色】
脑补一下给出的图:一个环,然后有若干连接环点的边,我们就是要求这些边不重叠 考虑一下不重叠的情况,两个有交边一定要一个在环内一个在环外,所以把相交的边连边,然后跑黑白染色看是否能不矛盾即可(可能算个2 ...
- 装载:关于拉格朗日乘子法与KKT条件
作者:@wzyer 拉格朗日乘子法无疑是最优化理论中最重要的一个方法.但是现在网上并没有很好的完整介绍整个方法的文章.我这里尝试详细介绍一下这方面的有关问题,插入自己的一些理解,希望能够对大家有帮助. ...
随机推荐
- 福大软工1816 - 404 Note Found选题报告
目录 NABCD分析引用 N(Need,需求): A(Approach,做法): B(Benefit,好处): C(Competitors,竞争): D(Delivery,交付): 初期 中期 个人贡 ...
- struts2 action json(还有servlet的)
http://yshjava.iteye.com/blog/1333104 留着 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 读 《我是一只IT小小鸟》 有感
在没有上大学之前,我很迷茫自己将来要从事什么行业.有人说,人生的每一个阶段都应该有自己的目标,然而,我上大学之前,甚至大一下学期之前,我对于我今后的从业道路,人生规划,都是迷茫的.高考结束成绩出来后, ...
- StringBuilder、StringBuffer和String三者的联系和区别
String 类 String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. String a = "a ...
- lintcode-208-赋值运算符重载
208-赋值运算符重载 实现赋值运算符重载函数,确保: 新的数据可准确地被复制 旧的数据可准确地删除/释放 可进行 A = B = C 赋值 说明 本题只适用于C++,因为 Java 和 Python ...
- Servlet中常用对象及API类之间的关系
Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...
- [历史百科]抗战时期兵团简介 From 百度知道
中央军委1948年11月1日和1949年1月15日两次关于统一全军组织和部队番号的训令,我军先后进行了整编.西北野战军改称第一野战军,司令员兼政治委员彭德怀,第一副司令员张宗逊,第二副司令员赵寿山,参 ...
- 中小型园区网络的基本部署之动手划分vlan
在计算机网络中,一个二层网络可以被划分为多个不同的广播域,一个广播域对应了一个特定的用户组,默认情况下这些不同的广播域是相互隔离的.不同的广播域之间想要通信,需要通过一个或多个路由器.这样的一个广播域 ...
- 简单配置AAA认证与telnet图解
不罗嗦,直接上图
- Golang的第一个程序-Hello, World !
安装Golang: 1. 下载安装包 https://golang.google.cn/dl/ 我这里使用压缩包,下载后解压到D盘(自定义). 2. 添加环境变量:把解压后的bin目录添加到环境变量中 ...