HDU 3715 Go Deeper 【二分+2-SAT】


传送门:HDU 3715


Go Deeper
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem 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. a, b, c 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 a, b and c are m while the length of array x is n. Given the elements of array a, b, and c, when we call the procedure go(0, n, m) what is the maximal possible value the procedure may 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-1, bi-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数组只有0,1两个值,C数组只有0,1,2两个值,对于C的每个值,有如下关系:

  • C==0,X[a[dep]]+X[b[dep]]!=0,及X[a[dep]],X[b[dep]]不能都是为0
  • C==1,X[a[dep]]+X[b[dep]]!=1,及X[a[dep]],X[b[dep]]要么都是0,要么都是1
  • C==2,X[a[dep]]+X[b[dep]]!=2,及X[a[dep]],X[b[dep]]不能都是1

用上面的三种情况的矛盾建边,二分最多递归的层数。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll long long
using namespace std;
const int N=1100;
const int M=11000;
int t,n,m,top;
bool mark[M];
int stack[M];
vector<int> g[M];
int a[M],b[M],c[M];
void init()
{
  memset(mark,false,sizeof(mark));
  for(int i=0;i<M;i++)
    g[i].clear();
}
void AddEdge(int x,int y)
{
  g[x].push_back(y);
}
bool dfs(int x)
{
  if(mark[x^1]) return false;
  if(mark[x]) return true;
  mark[x]=true;
  stack[++top]=x;
  int len=g[x].size();
  for(int i=0;i<len;i++)
  {
    if(!dfs(g[x][i])) return false;
  }
  return true;
}
bool twosat()
{
  for(int i=0;i<2*n;i+=2)
  {
    if(!mark[i]&&!mark[i+1])
    {
      top=0;
      if(!dfs(i))
      {
        while(top>0)
        {
          mark[stack[top--]]=false;
        }
        if(!dfs(i+1)) return false;
      }
    }
  }
  return true;
}
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
      scanf("%d%d%d",&a[i],&b[i],&c[i]);
    }
    int l=1,r=m;
    while(l<=r)
    {
      init();
      int mid=(l+r)/2;
      for(int i=1;i<=mid;i++)
      {
        if(c[i]==0)//xa,xb不能都是0
        {
          AddEdge(2*a[i],2*b[i]+1);
          AddEdge(2*b[i],2*a[i]+1);
        }
        if(c[i]==1)//xa,xb要么都是0,要么都是1
        {
          AddEdge(2*a[i]+1,2*b[i]+1);
          AddEdge(2*b[i]+1,2*a[i]+1);
          AddEdge(2*a[i],2*b[i]);
          AddEdge(2*b[i],2*a[i]);
        }
        if(c[i]==2)//xa,xb不能都是1
        {
          AddEdge(2*a[i]+1,2*b[i]);
          AddEdge(2*b[i]+1,2*a[i]);
        }
      }
      if(twosat())
      {
        l=mid+1;
      }
      else r=mid-1;
    }
    printf("%d\n",l-1);
  }
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值