二分答案,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的更多相关文章

  1. LA 5010 Go Deeper 2-SAT 二分

    题意: 有\(n\)个布尔变量\(x_i\),有一个递归函数.如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层. 问最多能递归多少层. 分析 ...

  2. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  3. zoj3422Go Deeper(2-sat + 二分)

    题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...

  4. Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description ...

  5. HDU 3715 Go Deeper(2-sat)

    HDU 3715 Go Deeper 题目链接 题意:依据题意那个函数,构造x数组.问最大能递归层数 思路:转化为2-sat问题,因为x仅仅能是0.1,c仅仅能是0,1.2那么问题就好办了,对于0, ...

  6. 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 ...

  7. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  8. UVALive - 3211 - Now or later(图论——2-SAT)

    Problem   UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...

  9. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

随机推荐

  1. Android学习笔记(二)

    Google在Android4.0之后加入了Action Bar的功能.但是有时候标题栏会相当占用屏幕空间,使得内容区域变小,这里演示如何隐藏标题栏. 隐藏标题栏的方法很简单,打开上节的FirstAc ...

  2. rsync+sersync实现文件实时同步

    前言: 一.为什么要用Rsync+sersync架构? 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增 ...

  3. bootstrap-列表组

    <div class="container"> <!-- list-group 列表组 给ul添加 list-group-item 列表项 给li添加 --> ...

  4. 自制公众平台Web Api(微信)

    最近一段时间感觉没什么东西可以分享给大家,又由于手上项目比较赶,不太更新博客了,今天趁着生病闲下来的时间分享一些项目中的东西给大家. 公众平台 提起公众平台当下最流行的莫过于腾讯的微信了,当然还有易信 ...

  5. Makefile 自动化变量

    Makefile中常用自动化变量解释如下: $@------规则的目标文件名 $<------规则的第一个依赖项文件名 $^------规则的所有依赖文件列表,以空格隔开. $?-------所 ...

  6. HDOJ2222 Keywords Search-AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  7. window下乌龟git安装和使用

    一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 图太多就不继续了~~ ...

  8. Fragment一些问题

    1.使用fragment静态加载,当需要替换的时候使用replace方式是无效的....... 2.replace的容器如果是线性布局,那么将会出现之前的页面残留的情况,正确做法是使用FrameLay ...

  9. xml 中转意字符&\/使用方法

    所有 XML 元素都须有关闭标签 在 HTML,经常会看到没有关闭标签的元素: <p>This is a paragraph <p>This is another paragr ...

  10. 【转】关于LWF——线性工作流

    1.什么是LWF? LWF全称Linear Workflow,中文翻译为线性工作流.“工作流”在这里可以当作工作流程来理解.LWF就是一种通过调整图像Gamma值,来使得图像得到线性化显示的技术流程. ...