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个 ...
随机推荐
- JavaScript 当月第一天和最后一天
1. 概述 1.1 说明 在项目过程中,有时候需要默认展示一个月的查询条件,即当月的第一天和最后一天. 2. 代码 2.1 代码示例 直接调用getFirstAndLastDay()即可得到当月的第一 ...
- js变量传递
基本类型.引用类型 基本类型: undefined.Null.Boolean.Number.String五种 (简单的数据段);引用类型: object (由多个值构成). 两种类型在使用上的区别: ...
- KVM 热迁移
最终我们迁移的目的就是: ·简化系统维护管理 ·高系统负载均衡 ·增强系统错误容忍度 ·优化系统电源管理 热迁移 又叫动态迁移,实时迁移,即虚拟机保存( save )/恢复( restore ):将整 ...
- 末学者笔记--Linux权限管理
一.权限概述 Linux系统一般将文件可存/取访问的身份分为3个类别:owner(拥有者).group(和所有者同组的用户).others(其他人,除了所有者,除了同组的用户以及除了超级管理员),且3 ...
- python的学习之路(二)
1.字符串内置功能练习#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harsonname = 'harson'name =str('harso ...
- requireJS简单应用
项目结构目录: Html页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- 编写一份好的 Vimrc
编写一份好的 Vimrc 目录 如何 Vimrc 色彩 空白字符与制表符 UI 配置 搜索 折叠 移动 用户自定义的前缀快捷按键 插件CtrlP 启动配置 终端Tmux 自动命令及其分组 备份 自定义 ...
- 今日报错Cannot access java.lang.String
public java.long.Long getId() { return id; } public void setId(java.lang.Long id) { this.id = id;} 手 ...
- ImCash:韩国最大交易所遭遇至暗时刻:2018年亏损1.8亿美元
Bithumb上个月遭到黑客攻击,随后要求用户小心存款,该公司报告称损失1.8亿美元(合2050亿韩元). 据<韩国时报>(Korea Times)报道:受到熊市影响,数字货币交易所实际交 ...
- 源自于NEO的KeyValue 数据库面世啦
虽然想把标题取得大一点,但终究不是什么太大不了的工作,还是安分守己的开始介绍吧. 项目组成 这个项目叫做LightDB 由三个部分构成 Lightdb.lib 是对rocksdb做了一层封装, ...