UVALive 5010 Go Deeper 2sat
二分答案,2sat判定。
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("in.txt","r",stdin);
//freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn=;
struct TwoSat
{
int n;
vector<int> g[maxn*];
bool mark[maxn*];
int s[maxn*],c;
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_clause(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]]=;
if(!dfs(i+))return false;
}
}
}
return true;
}
}; TwoSat solver; const int maxm=;
int a[maxm],b[maxm],c[maxm];
int n,m;
int check(int k)
{
solver.init(n);
for(int i=;i<k;i++)
{
if(c[i]==)
{
int x=a[i],y=b[i];
solver.add_clause(x,,y,);
}
if(c[i]==)
{
int x=a[i],y=b[i];
solver.add_clause(x,,y,);
}
if(c[i]==)
{
int x=a[i],y=b[i];
solver.add_clause(x,,y,);
solver.add_clause(x,,y,);
}
}
return solver.solve();
}
int main()
{
int t;
scanf("%d",&t);
for(int ca=;ca<=t;ca++)
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
int l=,r=m+;
while(l<r)
{
int mid=(r+l)>>;
if(check(mid))
l=mid+;
else r=mid;
}
printf("%d\n",l-);
}
return ;
}
UVALive 5010 Go Deeper 2sat的更多相关文章
- LA 5010 Go Deeper 2-SAT 二分
题意: 有\(n\)个布尔变量\(x_i\),有一个递归函数.如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层. 问最多能递归多少层. 分析 ...
- UVALive 3713 Astronauts (2-SAT,变形)
题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...
- zoj3422Go Deeper(2-sat + 二分)
题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...
- Go Deeper(2010成都现场赛题)(2-sat)
G - Go Deeper Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- HDU 3715 Go Deeper(2-sat)
HDU 3715 Go Deeper 题目链接 题意:依据题意那个函数,构造x数组.问最大能递归层数 思路:转化为2-sat问题,因为x仅仅能是0.1,c仅仅能是0,1.2那么问题就好办了,对于0, ...
- POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...
- UVALive - 3713 - Astronauts(图论——2-SAT)
Problem UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...
- UVALive - 3211 - Now or later(图论——2-SAT)
Problem UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
随机推荐
- FullCalendar只可以从外部拖入,内部不能互相拖动
startDrag: function(ev) { if(ev.originalEvent.initEvent){ return; } if (!this.isListening) { // star ...
- Asp.net MVC 之过滤器
整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...
- git 服务器的搭建
文章转载地址:http://www.linuxidc.com/Linux/2015-05/117502.htm 亲测可行,已经自己搭建一个正在使用中,搭建完成后,结合着pycharm +git,就能直 ...
- mysql关于or的索引问题
摘自: http://www.educity.cn/wenda/590849.html http://blog.csdn.net/hguisu/article/details/7106159 问: 不 ...
- 树莓派 自身摄像头的opencv调用
之前写过一篇随笔关于树莓派3上摄像头的调用,使用的方式是安装v4l2驱动. 实际上有一种更加简单的方法. 树莓派中的camera module是放在/boot/目录下以固件的形式加载的,不是一个标准的 ...
- ubuntu 下rar解压工具安装方法
1.压缩功能安装 sudo apt-get install rar卸载 sudo apt-get remove rar2.解压功能安装 sudo apt-get install unrar卸载 sud ...
- I2C控制器的Verilog建模之二
前言:接着上一篇的I2C写操作,今天要实现一个I2C的读操作.虽然在ADV7181B配置内部寄存器时没有必要使用到读操作,但是为了进一步确认寄存器是否在I2C写模块下被正确配置,这一步是必不可少的. ...
- Laxcus大数据管理系统单机集群版
Laxcus大数据管理系统是我们Laxcus大数据实验室历时5年,全体系全功能设计研发的大数据产品,目前的最新版本是2.1版本.从三年前的1.0版本开始,Laxcus大数据系统投入到多个大数据和云计算 ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- nodejs Express 4.x req.body req.query req.params 三种获取参数的方法
第一种情况:http://localhost:3000/1,我们可以用req.params.(应该是跟路由有关,待) 第二种情况:http://localhost:3000/?id=1,用req.qu ...