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 ...
随机推荐
- DevExpress中GridView上的右键菜单
1. 先拖一个PopupMenu和BarManage控件,设置PopupMenu的Manager属性为BarManager. 2. 先选中GridView,不是GridControl,在属性窗口中,选 ...
- centos 7 下安装Nginx
下载Nginx wget nginx.tar.gz http://nginx.org/download/nginx-1.11.3.tar.gz 解压源码 .tar.gz 然后进入目录编译安装 cd n ...
- 技术那么多,你想看看JSON Schema的测试吗?
目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Jav ...
- ThinkPHP 3.2.3 Pager分页
不是很喜欢TP的分页类,因为生成的分页url感觉有点不好理解,例如访问路径xxxx/home/show.html,在模板输出分页后,例如产生了页码,页码链接的路径会变成xxxx/home/show/p ...
- 15. Linked List Cycle && Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
- [VB.NET]取消按钮按下的默认事件响应
大家应该有过这样的经历:有两个文本框a,b.通过编程,当我们在A中回车后,光标会移动到B文本框. 但是,不可避免的会听到一声“铛”的声音. 解决办法: 在Keydown或者KeyUp事件中,设置e.S ...
- weblogic 优化设置 http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html
引自:http://wenku.baidu.com/view/c42e7a5bbe23482fb4da4cf2.html
- 2016年11月26号随笔(关于oracle数据库)
今天写了几个小时的sql语句,一开始我并没有思路,有思路便开始写. 首先我查询了入库表中的3级单位下的各个网点的入库信息,找到这些信息后,我又去入库明细表中查询入库的详细信息 找到了我要的把捆包箱的各 ...
- LINUX下查看负载
1,查看磁盘 df -h 2,查看内存大小 free free [-m|g]按MB,GB显示内存 vmstat 3,查看cpu cat /proc/cpuinfo 只看cpu数量grep " ...
- A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war
完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...