UVALive 3211 Now or later(2-sat)
2-sat问题,一种在两种可能性中选择必然关系的问题。
推荐两篇论文,也是学2-sat公认比较好的材料。前者较好理解,后者需耐心看。
http://bbs.byr.cn/wForum/boardcon.php?bid=212&id=15890&ftype=3&ap=278
“最小值尽量大”问题,又是一种经典模型,二分答案,从而判断是否能构建出合适的方案。
代码看书码的,注意几点:
不能缩点,会把不成立的情况缩掉,所以改为标记。
一旦任意一点的两种可能性都不成立,即不存在完整的方案。
本题只是二选一,A或B的一种形式来建边的,其他形式的2-sat要通过做题去接触,不过,总的来说都是根据必然关系建图。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std; const int MAXN=; int T[MAXN][],n;
bool mark[MAXN<<];
int S[MAXN<<],c;
vector<int >G[MAXN<<]; void init(int n)
{
for(int i=;i<(n<<);i++)
G[i].clear();
memset(mark,,sizeof(mark));
} void add(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 dfs(int x)
{
if(mark[x^])
return false;
if(mark[x])
return true;
S[c++]=x;
mark[x]=true;
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;
} int test(int p)
{
init(n);
for(int i=;i<n;i++)
for(int a=;a<;a++)
for(int j=i+;j<n;j++)
for(int b=;b<;b++)
if(abs(T[i][a]-T[j][b])<p)
add(i,a,j,b);
return solve();
} int main()
{
while(~scanf("%d",&n))
{
int l,r;
l=r=;
for(int i=;i<n;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&T[i][j]);
r=max(r,T[i][j]);
}
}
while(l<r)
{
int m=l+(r-l+)/;
if(test(m))
l=m;
else
r=m-;
}
printf("%d\n",l);
}
return ;
}
UVALive 3211 Now or later(2-sat)的更多相关文章
- UVALive - 3211 - Now or later(图论——2-SAT)
Problem UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- UVALive 3211 Now or Later (2-SAT)
题目的要求一个最小值最大,二分即可,但是怎么判断呢? 飞机早或者晚两种状态,可以用一个布尔变量表示,假设当前猜测为m,那么根据题意, 如果x和y所对应的时间冲突那么就是¬(xΛy)化成或的形式(¬x) ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)
题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...
- UVALive 5903 Piece it together(二分图匹配)
给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB <==碎片 W 题目一看,感觉是精确覆盖(最近 ...
- UVALive 2403 77377解题报告(深搜)
题意:给你一些固定的字符串,在给出数字,根据键盘的对应关系,输出所有的满足条件的字符串,输出顺序无所谓. 思路:因为题目说了,输出比较小,说明测试数据并不强,所以可以暴力回溯求出答案,将所有的给出的字 ...
随机推荐
- ios 框架学习笔记
ios主要的系统层次: 一.Cocoa Touch 层:创建应用程序主要使用的框架. 1.关键技术: AirDrop:实现应用间通信. Text Kit:处理文本和排版. UIKit Dynamics ...
- [转载]C#中int和IntPtr相互转换
方法一. int转IntPtr int i = 12; IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p; ...
- Unity3D角色攻击范围判定和攻击判定
原地址:http://www.unity蛮牛.com/blog-1801-479.html 第一种方法:运用点乘 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- LINUX下如何查看tomcat运行状态,判断其是否启动
1,查看Tomcat启动日志. ${catalina_home}\logs [root@iZ25b4ffkfaZ logs]# tail -f catalina.outSep 10, 2015 11: ...
- Openfire 代码部署报错: Variable references non-existent resource:${workspace_loc:openfire_src}
Variable references non-existent resource:${workspace_loc:openfire_src} -DopenfireHome=“${workspace_ ...
- 两个List合并,过滤重复记录
import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...
- Chrome中的消息循环
主要是自己做个学习笔记吧,我经验也不是很丰富,以前学习多线程的时候就感觉写多线程程序很麻烦.主要是线程之间要通信,要切线程,要同步,各种麻烦.我本身的工作经历决定了也没有太多的工作经验,所以chrom ...
- Java API —— BigInteger类
1.BigInteger类概述 可以让超过Integer范围内的数据进行运算 2.构造方法 public BigInteger(String val) 3.BigInteger类 ...
- JSF开篇之Login案例
开发环境:Myeclipse+JDK5+MyEclipse Tomcat+jsf2.2.8 JSF看起来和STRUTS还是有些像的,刚开始还是遇到一点问题:资源包的存放路径及文件访问路径. 开发Log ...
- Hibernate学习笔记(1)
1 使用Hibernate (1)创建User Library,命名为HIBERNATE3,加入需要的jar (2)创建hibernate配置文件hibernate.cfg.xml, 为了便于调试最好 ...