HDU 3715 Go Deeper
二分答案 + 2-SAT判断
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=+;
int M,N,T;
int ans;
int L,R,Mid;
int a[],b[],c[]; 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 y)
{
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]]=false;
if(!dfs(i+)) return false;
}
}
return true;
}
}; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=;i<M;i++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
L=,R=M; while(L<=R)
{
Mid=(L+R)/;
TwoSAT T; T.init(N);
for(int i=;i<Mid;i++)
{
if(c[i]==)
{
T.add_clause(*a[i]+,*b[i]+);
}
else if(c[i]==)
{
T.add_clause(*a[i]+,*b[i]);
T.add_clause(*a[i],*b[i]+);
}
else if(c[i]==)
{
T.add_clause(*a[i],*b[i]);
}
}
if(T.solve())
{
ans=Mid;
L=Mid+;
}
else R=Mid-;
}
printf("%d\n",ans);
}
return ;
}
HDU 3715 Go Deeper的更多相关文章
- HDU 3715 Go Deeper(2-sat)
HDU 3715 Go Deeper 题目链接 题意:依据题意那个函数,构造x数组.问最大能递归层数 思路:转化为2-sat问题,因为x仅仅能是0.1,c仅仅能是0,1.2那么问题就好办了,对于0, ...
- Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
Go Deeper Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 3715(二分+2-sat)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3715 思路:二分深度,2-sat判断可行性,根据矛盾关系建图:设a=0,a'=1,b=0,b'=1;如 ...
- hdu 3715
一个很简单的2-sat的题: 不过比较难想到: 其实也不是很难,可能接触的少了吧! #include<cstdio> #include<vector> #define maxn ...
- HDU - 1816 Get Luffy Out *(二分 + 2-SAT)
题目大意:有N串钥匙,M对锁.每串钥匙仅仅能选择当中一把.怎样选择,才干使开的锁达到最大(锁仅仅能按顺序一对一对开.仅仅要开了当中一个锁就可以) 解题思路:这题跟HDU - 3715 Go Deepe ...
- 【图论】2-sat总结
2-sat总结 2-sat问题,一般表现的形式为.每一个点有两种方式a,b,要么选a,要么选b.而且点点之间有一些约束关系.比如:u和v至少一个选a.那么这就是一个表达式.把a当成真,b当成假,那就是 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
随机推荐
- Chapter 2 Open Book——14
I backpedaled. "They seemed nice enough to me. I just noticed they keptto themselves. 我改口说道,他们看 ...
- isset() 与 empty() 的区别
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- hdu_5676_ztr loves lucky numbers
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5676 在这%一下安神,用了我没见过的黑科技next_permutation,至少我是今天才知道的 #i ...
- Git从远程库克隆
上次我们讲了先有本地库,后有远程库,如何关联远程库. 现在,假设我们从零开始开发,那么最好的方式就是先创建远程库,然后从远程库克隆. 首先,登录GitHub,创建一个新的仓库,gitskill 创建过 ...
- Server对象(是属性)
html, body { font-size: 10.5pt; } body { font-family: 微软雅黑, Helvetica, "Hiragino Sans GB", ...
- js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
在本地用ajax跨域访问请求时报错: XMLHttpRequest cannot loadhttp://www.zjblogs.com/. No 'Access-Control-Allow-Origi ...
- Android onTouchEvent方法
onTouchEvent方法简介 前面已经介绍了手机键盘事件的处理方法,接下来将介绍手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法, ...
- 移植iw 到linux平台上。
https://github.com/174high/iw-3.7-Linux-porting https://github.com/174high/libnl-1.1-stable-master-l ...
- WPF Template模版之寻找失落的控件【三】
“井水不犯河水”常用来形容两个组织之间界限分明.互不相干,LogicTree与控件内部这颗小树之间就保持着这种关系.换句话说,如果UI元素树上有个X:Name=“TextBox1”的控件,某个控件内部 ...
- FZU 2113 BCD Code 数位dp
数位dp,但是很奇怪的是我在虚拟oj上用GUC C++提交会wa,用Visual c++提交正确,但是加上注释后提交又莫名CE--好任性啊 0 ,0 题目思路:看代码吧 注释很详细 #include& ...