2-sat基础题 uvalive 3211
蓝书325页的基础题
二分+2-sat
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#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 = + ; struct TwoSAT{
int n;
vector<int> G[maxn];
bool mark[maxn];
int c, s[maxn];///c是表示目前dfs到的个数和已经被标记的集合s
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;
} void init(int n){
this->n = n;
for (int i = ; i < * n; i++) G[i].clear();
memset(mark, , sizeof(mark));
} void add_edge(int x, int xval, int y, int yval){
x = x * + xval, y = y * + yval;
G[x ^ ].push_back(y);
G[y ^ ].push_back(x);
} 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 sat;
int n;
int E[maxn], L[maxn];
int ti[maxn][]; bool test(int p){
sat.init(n);
for (int i = ; i < n; i++){
for (int x = ; x < ; x++){
for (int j = i + ; j < n; j++){
for (int y = ; y < ; y++){
if (abs(ti[i][x] - ti[j][y]) < p){
///那就说明不成立,因此要建立成立的边
sat.add_edge(i, x ^ , j, y ^ );
}
}
}
}
}
return sat.solve();
} int main(){
while (scanf("%d", &n) == ){
int lb = , rb = ;
for (int i = ; i < n; i++){
scanf("%d%d", E + i, L + i);
ti[i][] = E[i], ti[i][] = L[i];
rb = max(rb, max(E[i], L[i]));
}
rb++;
while (lb < rb - ){
int mid = (lb + rb) / ;
if (test(mid)) lb = mid;
else rb = mid;
}
printf("%d\n",lb);
}
return ;
}
2-sat基础题 uvalive 3211的更多相关文章
- UVALive - 3211 - Now or later(图论——2-SAT)
Problem UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...
- UVALive - 3211 (2-SAT + 二分)
layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
- 小试牛刀3之JavaScript基础题
JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...
- 小试牛刀2:JavaScript基础题
JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
- linux面试题-基础题1
第1章 基础题1 1.1 在装系统创建Linux分区时,一般至少需要创建两个分区( ) A.FAT.NTFS B. /usr.swap C. /boot.swap D.swap./ 1.2 ...
- Java面试题以及答案精选(架构师面试题)-基础题1
基础题 一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的?1. String是字符串常量,StringBuffer和StringBu ...
随机推荐
- 软工实践-Alpha 冲刺 (5/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- whu Problem 1537 - A - Stones I 贪心
题目链接: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 Stones I Time Limit: 1000MSMemory Li ...
- C语言的知识与能力予以自评
看到一个问卷不错,拟作为第三次作业的部分内容. 你对自己的未来有什么规划?做了哪些准备?答:多学习几门生存技巧,首先先学会碰壁. 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?答:学习是人 ...
- 0517 SCRUM团队项目4.0
题目 1.准备看板.形式参考图4.2.任务认领,并把认领人标注在看板上的任务标签上.先由个人主动领任务,PM根据具体情况进行任务的平衡.然后每个人都着手实现自己的任务.3.为了团队合作愉快进展顺利,请 ...
- texbbox,combobox设置属性
--输入框 $("#xx").textbox('setValue','value'); //设置输入框的值 $('#xx').textbox('textbox').attr('r ...
- nginx 二进制安装
Nginx的安装方法 1:yum安装 默认是1.6版本,且在epel源中 2:源码包编译安装 源码下载:http://nginx.org/en/download.html,下载1.8稳定版本 ...
- jetty之maven配置
<!-- jetty 插件配置 --><plugin> <groupId>org.mortbay.jetty</groupId> <artifac ...
- Node.js系列——(4)优势及场景
背景 之前几篇系列文章简单介绍了node.js的安装配置及基本操作: Node.js系列--(1)安装配置与基本使用 Node.js系列--(2)发起get/post请求 Node.js系列--(3) ...
- Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错,
Linux下编译程序时,经常会遇到“undefined reference to XXX” 报错, 这里总结一些可能的原因和解决方案,给需要的朋友: 说道undefined reference err ...
- PHP对象的复制
对象的复制(克隆) $obj1 = new A(); $obj1->p1 = 11; $obj2 = $obj1; //值传递 //则,现在有几个对象?——1个对象! 当然: $obj3 ...