2sat
之前做的两发
https://vjudge.net/problem/UVALive-3211
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
int t[maxn][], n, tot, S[maxn*], scnt;
bool mark[maxn*];
struct Edge{
int v, nxt;
Edge(){}
Edge(int v, int nxt):v(v), nxt(nxt){}
}edge[maxn*maxn*];
int head[maxn*];
void addedge(int u, int v){
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
}
void conj(int x, int detx, int y, int dety){
x = *x+detx;
y = *y+dety;
addedge(x^, y);
addedge(y^, x);
}
void build(int mid){
tot = ;
memset(head, -, sizeof head);
for(int i = ; i < n; i++)
for(int a = ; a < ; a++)
for(int j = i+; j < n; j++)
for(int b = ; b < ; b++)
if(fabs(t[i][a]-t[j][b]) < mid)
conj(i, a^, j, b^); }
bool dfs(int x){
if(mark[x])
return true;
if(mark[x^])
return false;
mark[x] = true;
S[scnt++] = x;
for(int i = head[x]; ~i; i = edge[i].nxt){
int v = edge[i].v;
if(!dfs(v))
return false;
}
return true;
}
bool solve(){
memset(mark, false, sizeof mark);
for(int i = ; i < *n; i+=){
if(!mark[i]&&!mark[i+]){
scnt = ;
if(!dfs(i)){
for(int j = ; j < scnt; j++){
mark[S[j]] = false;
}
scnt = ;
if(!dfs(i+))
return false;
}
}
}
return true;
}
int main(){
while(scanf("%d", &n)== && n){
for(int i = ; i < n; i++)
for(int j = ; j < ; j++){
scanf("%d", &t[i][j]);
}
int l = , r = 1e7;
while(l < r){
int mid = (r-l+)/+l;
build(mid);
if(solve())
l = mid;
else
r = mid-;
}
printf("%d\n", l);
}
return ;
}
/*
10
44 156
153 182
48 109
160 201
55 186
54 207
55 165
17 58
132 160
87 197
*/
https://vjudge.net/problem/UVALive-3713
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
int age[maxn], n, m, tot, S[maxn*], scnt;
double ave;
bool mark[maxn*];
struct Edge{
int v, nxt;
Edge(){}
Edge(int v, int nxt):v(v), nxt(nxt){}
}edge[*];
int head[maxn*];
void addedge(int u, int v){
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
}
void conj(int x, int detx, int y, int dety){
x = *x+detx;
y = *y+dety;
addedge(x^, y);
addedge(y^, x);
}
bool dfs(int x){
if(mark[x])
return true;
if(mark[x^])
return false;
mark[x] = true;
S[scnt++] = x;
for(int i = head[x]; ~i; i = edge[i].nxt){
int v = edge[i].v;
if(!dfs(v))
return false;
}
return true;
}
bool solve(){
memset(mark, false, sizeof mark);
for(int i = ; i < *n; i+=){
if(!mark[i]&&!mark[i+]){
scnt = ;
if(!dfs(i)){
for(int j = ; j < scnt; j++){
mark[S[j]] = false;
}
scnt = ;
if(!dfs(i+))
return false;
}
}
}
return true;
}
int main(){
while(scanf("%d%d", &n, &m)){
tot = ;
memset(head, -, sizeof head);
if(!n&&!m)
break;
double sum = ;
for(int i = ; i < n; i++){
scanf("%d", &age[i]);
sum += age[i];
}
ave = sum/n;
while(m--){
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
if((age[u]<ave&&age[v]<ave) || (age[u]>=ave&&age[v]>=ave)){
conj(u, , v, );
conj(u, , v, );
}
else{
conj(u, , v, );
}
}
if(!solve()){
puts("No solution.");
}
else{
for(int i = ; i < *n; i+=){
if(mark[i]){
printf("C\n");
}
else{
if(age[i/]>=ave)
printf("A\n");
else
printf("B\n");
}
}
}
}
return ;
}
/*
16 20 21
22
23
24
25
26
27
28 101 102 103 104 105 106 107 108
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 1 10 2 9
3 12 4 11 5 14 6 13 7 16 8 15 1 12 1 13 3 16 6 15 0 0*/
由于拆点,点数应该是2n...
每个字句a|b 加边!a -> b, !b -> a, 两条边呢...
点和边开的不够大,一言不合就爆掉了...
2sat的更多相关文章
- 2-Sat问题
二分+2-Sat 判断是否可行 输出字典序最小的解 输出字典序可行解 其实这些都是小问题,最重要的是建图,请看论文. 特殊的建边方式,如果a b是一对,a必须选,那么就是b->a建边. HDU ...
- UVALive 4849 String Phone(2-sat、01染色)
题目一眼看去以为是4-sat... 题意:给n(n<=3000)个黑方块的坐标,保证黑方块没有公共边.对于每个黑方块选一个角作为结点,使得所选结点满足输入的一个无向图.其中距离为曼哈顿距离.输出 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...
- UVa 1391 Astronauts (2SAT)
题意:给出一些宇航员他们的年龄,x是他们的平均年龄,其中A任务只能给年龄大于等于x的人,B任务只能给小于x的人,C任务没有限制.再给出m对人,他们不能同任务.现在要你输出一组符合要求的任务安排. 思路 ...
- hdu 3622 Bomb Game(二分+2-SAT)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- zoj 3717 - Balloon(2-SAT)
裸的2-SAT,详见刘汝佳训练指南P-323 不过此题有个特别需要注意的地方:You should promise that there is still no overlap for any two ...
- [BZOJ 1997][HNOI2010]Planar(2-SAT)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1997 分析: 考虑每条边是在圈子里面还是圈子外面 所以就变成了2-SAT判定问题了= ...
- 【POJ 3062】Party(2-SAT、tarjan)
2-SAT的入门题. a,a',b,b'分别表示两对夫妇,如果a,b有矛盾,那么a要来,就只能来b',b要来,就只能来a'.于是建了两条边(a,b'),(b,a'). 用tarjan强连通分量缩点染色 ...
- [模板] 2-SAT
昨天早上在准备省队集训,发现自己连2-SAT是什么都不知道,于是一早上都投身于2-SAT模板中,终于有个结果. 思路如下: 1.根据条件表达式建边: 2.缩环: 3.判断是否可行: 4.根据缩完环的图 ...
- 2-SAT 问题
2-SAT 问题是k-SAT问题在k==2时的特殊情况,因为已经证明k>=3时的k-sat问题属于npc问题.所以在这里仅研究2-SAT的特殊情况. 何为2-sat问题? 简单地说就是有N个 ...
随机推荐
- HTTP协议详解(三)
接着第二篇继续学习... 6 HTTP的几个重要概念 6.1连接:Connection 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1,request和reponse头中都 ...
- 先安装VS后安装IIS,注册IIS方法
IIS和VS安装顺序: 最好的顺序是先安装IIS,然后安装VS. 因为在安装VS的时候,安装程序会自动检测计算机上是否安装IIS,若有,则将.NET的当前版本注册到IIS,比如安装VS2010,VS2 ...
- 实验1:C++简单程序设计(1)
实验目的 1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构 2. 掌握C++中数据输入和输出的基本方法 3. 熟练使用c++程序开发环境,掌握c++程序 ...
- ActiveMQ简单使用
// 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. //brokerURL服务器的ip及端口号 ConnectionFactory connectionFactor ...
- python 视频转成代码视频
# -*- coding:utf-8 -*- # coding:utf-8 import os, cv2, subprocess, shutil from cv2 import VideoWriter ...
- Lua模式匹配
Lua并不使用POSIX规范的正则表达式[4](也写作regexp)来进行模式匹配.主要的原因出于程序大小方面的考虑:实现一个典型的符合POSIX标准的regexp大概需要4000行代码,这比整个Lu ...
- MySQL 的安装与使用(一)
一.Windows 上安装 MySQL 1.Windows 上安装 MySQL 相对来说会较为简单,地那就链接 https://cdn.mysql.com//Downloads/MySQL-8.0/m ...
- Vue:window.onresize
1. 添加属性screenHeight 和 timer. screenHeight: window.innerHeight timer: '' // window.onresize函数频繁调用时,页 ...
- codeblocks 配置 opengl 编程宝典 的 gltools 环境
懒得多说,亲测,这个问题,csdn 和 cnblog 上的博客真的没有一个能解决的. 这个帖子2L的答案则完美解决了问题,虽然步骤有些繁琐,过程还是英文,但考虑到了可能出现的各种问题,跟着走一遍就完美 ...
- java----代码打包
打包 文件生成在out目录下 D:\IDEA代码\out\artifacts\IDEA_jar 注意打包好像只能打包src下面的代码 不在src目录下的一些文件,自己文件添加到打包好的目录下 可以选择 ...