条件转化,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 拉格朗日乘子法无疑是最优化理论中最重要的一个方法.但是现在网上并没有很好的完整介绍整个方法的文章.我这里尝试详细介绍一下这方面的有关问题,插入自己的一些理解,希望能够对大家有帮助. ...
随机推荐
- 四则运算3+psp0
题目要求: 1.程序可以判断用户的输入答案是否正确,如果错误,给出正确答案,如果正确,给出提示. 2.程序可以处理四种运算的混合算式. 3.要求两人合作分析,合作编程,单独撰写博客. 团队成员:张绍佳 ...
- VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp
题目链接: http://codeforces.com/problemset/problem/533/B B. Work Group time limit per test2 secondsmemor ...
- this & super
/* 当本类的成员和局部变量同名用this区分. 当子父类中的成员变量同名用super区分父类. this和super的用法很相似. this:代表一个本类对象的引用. super:代表一个父 ...
- 三层神经网络自编码算法推导和MATLAB实现 (转载)
转载自:http://www.cnblogs.com/tornadomeet/archive/2013/03/20/2970724.html 前言: 现在来进入sparse autoencoder的一 ...
- 【beta】nice!-------约吧NABCD
小组名称:nice! 组长:李权 成员:于淼 刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 约吧APP下载地址: 百度云:链接:http://pan.baidu.com/s/1jHNBR3g ...
- c# assembly
string path = @"c:\text.dll" Assembly assembly = Assembly.LoadFile(path); path = "MyP ...
- Spring MVC @RequestParam @RequestHeader @CookieValue用法
摘要: package com.hust.springmvc1; import org.springframework.stereotype.Controller; import org.spring ...
- Building simple plug-ins system for ASP.NET Core(转)
Recently I built plug-ins support to my TemperatureStation IoT solution web site. The code for .NET ...
- 第88天:HTML5中使用classList操作css类
在HTML5 API里,页面DOM里的每个节点上都有一个classList对象,程序员可以使用里面的方法新增.删除.修改节点上的CSS类.使用classList,程序员还可以用它来判断某个节点是否被赋 ...
- CodeForces 860D Wizard's Tour
题意 给出一张无向图,要求找出尽量多的长度为2的不同路径(边不可以重复使用,点可以重复使用) 分析 yzy:这是原题 http://www.lydsy.com/JudgeOnline/problem. ...