二分答案 + 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 ;
}

最新文章

  1. unity打包模型存在的一个问题
  2. CentOS7.2安装总结
  3. python函数基础以及函数参数简解
  4. request重定向或者是response转发请求后面的代码依然执行
  5. cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案
  6. iOS- iPad UIPopoverController
  7. Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused b
  8. 802.11(wi-fi)的PHY层(编码与调制方法)
  9. mvc+EF比较好的框架
  10. JS里charCodeAt()和fromCharCode()方法拓展应用:加密与解密
  11. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
  12. 从零开始学安全(三十三)●Ununtu16 LMAP 环境搭建
  13. centos7中/tmp文件保存天数
  14. (三)ajax请求不同源之cors跨域
  15. ACM(数学问题)——UVa202:输入整数a和b(0≤a≤3000,1≤b≤3000),输出a/b的循环小数表示以及循环节长度。
  16. java将秒转换为时分秒工具类
  17. BZOJ3862 Little Devil I 树链剖分
  18. java8 使用 lamda 表达式 完成 map reduce
  19. Laravel Nginx 站点配置文件(Homestead)
  20. 【NOIP题解】NOIP2017 TG D2T3 列队

热门文章

  1. linux配置java环境变量(转)
  2. hdu_4521_小明系列问题——小明序列(LIS)
  3. linux top 命令---VIRT,RES,SHR,虚拟内存和物理内存(
  4. uCOS-II的信号量及使用
  5. File类与FileInfo类区别
  6. 实参时丢弃了类型 discards qualifiers discards qualifiers问题
  7. Android sdk content loader
  8. java获取程序执行时间
  9. OpenGL与vs编程——error C2440: “glMaterialfv”: 无法从“GLfloat”转换为“const GLfloat *”
  10. 设计模式-中介者模式(Mediator)