G - Go Deeper

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Here is a procedure's pseudocode:

 
	   go(int dep, int n, int m)
begin
output the value of dep.
if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m)
end

In this code n is an integer. abc and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths of array ab and c are mwhile the length of array x is n.

Given the elements of array ab, and c, when we call the procedure go(0, n , m) what is the maximal possible value does the procedure output?

Input

There are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers follow. The i-th(1 ≤ i ≤m) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1bi-1 < n, 0 ≤ ci-1 ≤ 2).

Output

For each test case, output the result in a single line.

Sample Input

3
2 1
0 1 0
2 1
0 0 0
2 2
0 1 0
1 1 2

Sample Output

1
1
2 题目大意:
 
给定一些方程,x[]数组未知,求前面最多能够有多少方程x[a]+x[b]!=c能够被满足。
 其中 c=0,1,2
  x[]={0,1}
相当于裸的2sat问题,加上二分
  
  强烈建议阅读 kuangbin大神对2-sat的总结:http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712429.html 总的来说,就是当 a or b 时 连接 a' -> b 与 b' -> a 的边,然后进行强连通判断是否出现 a 与 a' ...在同一个连通分量中,若在则不可能。 建立数a的两个状态,即a与a',相当于x[a]=1 和 x[a']=0 x[a]+x[b]!=0 => a or b => a'->b 且 a->b'
x[a]+x[b]!=1 => (a and b) or (a' and b') == a or b' 且 a' or b => a'->b' 且 b->a 且 a->b 且 b'->a
x[a]+x[b]!=2 => a' or b' => a->b' 且 a'->b 按上面来建图判断即可
#include<cstdio>
#include<cstring>
int e[],pd[],be[],ne[],all;
int dfn[],low[],instack[],belong[],stack[],stak,curr,num;
int a,b,c,n,m,l,r,mid,flag;
void add(int x,int y,int p){
e[++all]=y;
pd[all]=p;
ne[all]=be[x];
be[x]=all;
}
void tarjan(int x){
instack[x]=;
stack[++stak]=x;
dfn[x]=low[x]=++curr;
for(int j=be[x];j!=;j=ne[j])
if(pd[j]<=mid){
if(!dfn[e[j]]){
tarjan(e[j]);
if(low[x]>low[e[j]]) low[x]=low[e[j]];
}else if(instack[e[j]]&&low[x]>low[e[j]])
low[x]=low[e[j]];
}
if(dfn[x]==low[x]){
int j;
++num;
do{
j=stack[stak--];
instack[j]=;
belong[j]=num;
}while(j!=x);
}
}
int solve(){
curr=stak=num=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(instack,,sizeof(instack));
for(int i=;i<*n;i++)
if(!dfn[i]) tarjan(i);
flag=;
for(int i=;i<n;i++)
if(belong[*i]==belong[*i+]){
flag=;
break;
}
return flag;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--){
scanf("%d%d",&n,&m);
all=;
memset(e,,sizeof(e));
memset(be,,sizeof(be));
memset(ne,,sizeof(ne));
memset(pd,,sizeof(pd));
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
switch (c){
case : add(*a+,*b,i);
add(*b+,*a,i);
break;
case : add(*a,*b,i);
add(*b+,*a+,i);
add(*b,*a,i);
add(*a+,*b+,i);
break;
case : add(*a,*b+,i);
add(*b,*a+,i);
break;
}
}
l=;r=m-;
while(l<r-){
mid=(l+r)/;
if(solve()) r=mid; else l=mid;
}
mid=r;
if(!solve()) printf("%d\n",r+);
else printf("%d\n",l+);
}
return ;
}

Go Deeper(2010成都现场赛题)(2-sat)的更多相关文章

  1. Error Curves(2010成都现场赛题)

    F - Error Curves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

  2. 2011 ACM/ICPC 成都赛区(为2013/10/20成都现场赛Fighting)

    hdu 4111  Alice and Bob 博弈:http://www.cnblogs.com/XDJjy/p/3350014.html hdu 4112 Break the Chocolate ...

  3. HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)

    Isabella's Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hdu 4788 (2013成都现场赛 H题)

    100MB=10^5KB=10^8B 100MB=100*2^10KB=100*2^20B Sample Input2100[MB]1[B] Sample OutputCase #1: 4.63%Ca ...

  5. hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)

    简单概率题,可以直接由剩余n个递推到剩余0个.现在考虑剩余x个概率为(1-p)的candy时,概率为C(2 * n - x, x) * pow(p, n + 1)  *pow(1 - p, n - x ...

  6. hdu 4465 Candy 2012 成都现场赛

    /** 对于大数的很好的应用,,缩小放大,,保持精度 **/ #include <iostream> #include <cmath> #include <algorit ...

  7. hdu 4472 Count (2012 ACM-ICPC 成都现场赛)

    递推,考虑到一n可以由i * j + 1组合出来,即第二层有j个含有i个元素的子树...然后就可以了.. #include<algorithm> #include<iostream& ...

  8. 2013杭州现场赛B题-Rabbit Kingdom

    杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...

  9. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...

随机推荐

  1. [原创] zabbix学习之旅三:agent安装

    部署完zabbix server后,自然要部署zabbix agent.在官方描述中,agent是部署在被监控的机器上,用于采集CPU.内存.磁盘等统计信息,并上报给server用于进一步处理.age ...

  2. 3640: JC的小苹果 - BZOJ

    让我们继续JC和DZY的故事.“你是我的小丫小苹果,怎么爱你都不嫌多!”“点亮我生命的火,火火火火火!”话说JC历经艰辛来到了城市B,但是由于他的疏忽DZY偷走了他的小苹果!没有小苹果怎么听歌!他发现 ...

  3. Timer 的缺陷

    java.util.Timer计时器有管理任务延迟执行("如1000ms后执行任务")以及周期性执行("如每500ms执行一次该任务").但是,Timer存在一 ...

  4. c++ 常用数据接口 set

    #include <set> #include <iostream> #include <string> int main(void) { std::set< ...

  5. C# 数据结构--排序[下]

    希尔排序(Shell Sort) 排序思想: 先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组.所有距离为d1的倍数的记录放在同一个组中.先在各组内进行直接插入排序:然后,取第二个增量d2 ...

  6. Unity3D脚本中文系列教程(三)

    http://dong2008hong.blog.163.com/blog/static/4696882720140302323886/ Unity3D脚本中文系列教程(二) 示,属性不被序列化或显示 ...

  7. 【转】前端图片该保存为什么格式?png or jpg?

    疑虑: 图片存储为web格式,该用什么格式保存呢?png?jpg?压缩比例该为多大?css spript的优劣?有时候我们可能会因为一张格式不正确的图片而导致设计品质的下降以及页面性能的降低.了解图片 ...

  8. POJ3126Prime Path

    http://poj.org/problem?id=3126 题意 : 给你两个四位数,都是素数,一个是初始素数x,一个是目标素数y,让你从x变成y,x每次只能改变1位数,来变成另外一个素数k,再改变 ...

  9. hdu 1812 Count the Tetris

    高精度+polya原理可以搞定 思路: 设边长为n的正方形,c种颜色.旋转只有 0,90,180,270度三种旋法.旋0度,则置换的轮换数为n*n旋90度,n为偶数时,则置换的轮换数为n*n/4,n为 ...

  10. http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html

    http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html http://www.cnblogs.com/madyina/p/370 ...