uva 1146(2-sat)

刚开始学,具体题解看紫书325页

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
#define hh printf("=======================\n");
const int maxn=10000+100;
struct Twosat
{
   vector<int> g[maxn*2];
   bool mark[maxn*2];
   int s[maxn*2];
   int n,c;
    void init(int n)
    {
        this->n=n;
         for(int i=0;i<2*n;i++)
            g[i].clear();
         memset(mark,0,sizeof(mark));
    }
     bool dfs(int u)
     {
          if(mark[u]) return true;
         if(mark[u^1]) return false;
          mark[u]=1;
          s[c++]=u;
           for(int i=0;i<g[u].size();i++)
             if(!dfs(g[u][i])) return false;
         return true;
     }
     void add_clause(int x,int xval,int y,int yval)
     {
         x=x*2+xval;
         y=y*2+yval;
         g[x^1].push_back(y);
         g[y^1].push_back(x);
     }
     bool solved()
     {
          for(int i=0;i<n*2;i+=2)
             if(!mark[i]&&!mark[i+1])
          {   c=0;
              if(!dfs(i))
              {
                  while(c) mark[s[--c]]=0;
                  if(!dfs(i+1)) return false;
              }
          }
          return true;
     }

};

Twosat tt;
int n;
int t[maxn][5];
bool test(int x)
{
     tt.init(n);
     for(int i=0;i<n;i++) for(int a=0;a<2;a++)
      for(int j=0;j<i;j++)  for(int b=0;b<2;b++)
         if(abs(t[i][a]-t[j][b])<x) tt.add_clause(i,a^1,j,b^1);
    return tt.solved();
}
int main()
{
    while(~scanf("%d",&n))
    {

        int r=0;
          for(int i=0;i<n;i++)
          {
              for(int a=0;a<2;a++)
              {
                  scanf("%d",&t[i][a]);
                   r=max(r,t[i][a]);
              }
          }
           int l=0;
           while(l<r)
           {
                int mid=l+(r-l+1)/2;
                 if(test(mid)) l=mid;
                 else r=mid-1;
           }
          printf("%d\n",l);
    }
    return 0;
}
时间: 2024-08-28 00:35:11

uva 1146(2-sat)的相关文章

UVA 1146 Now or later

二分时间+2sat 边加多了....RE了好久...... Now or later Time Limit: 9000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description As you must have experienced, instead of landing immediately, an aircraft sometimes waits

UVA 1146 - Now or later(2-SET)

UVA 1146 - Now or later 题目链接 题意:n个飞机,每个飞机有一个早到时间和一个晚到时间,问怎么安排飞机,使得飞机到的间隔的最小值最大 思路:二分答案,然后利用2-set去判断,如果两个飞机的两个时刻间隔比这个时刻小,那么就是表示不能同时满足这两个条件,就加一条xi^xj的边进去,然后利用2-SET判定一下 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include &l

uva 1146 Now or late (暴力2-SAT)

/* 裸地2-SAT问题 关键是模型转化 最小的最大 显然二分 关键是Judge的时候怎么判断 每个航班是早是晚直接影响判断 早晚只能选一个 如果我们定义bool变量xi表示 i航班是否早到 每个航班虚拟出两个点2*i 2*i+1 分别表示是否早到 然后就可以假设某个航班早到然后推导出一定连得某些边 然后就开始选点 尝试这个点选不选 看看最后是否合法 */ #include<iostream> #include<cstdio> #include<cstring> #in

QT 状态机详解 statemachine (转)

状态机顾名思义,应该有不同的状态在切换.上面状态机图中,我们提供了两种状态state1和state2.而状态的区分是由状态的属性来描述的,比如p1,p2…等等.从一个状态到另一个状态的转化,必须由触发条件来完成,上图state1到state2的状态转换由transition1来表示,state2到state1的状态转换由transition2来表示.如果希望在状态转换过程中有动画来展示,那么可以在transition1和transition2中加入动画效果animation1和animation

UVA 11488 Hyper Prefix Sets (Trie)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483 Hyper Prefix Sets Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodnes

uva 1564 - Widget Factory(高斯消元+逆元)

题目链接:uva 1564 - Widget Factory 题目大意:n种零件,m次工作日程,零件序号从1到n,给出m次工作日程的信息,x,s,e,表示生产了x个零件,从星期s开始到星期e(有可能是多个星期),然后给出生产的x个零件的序号.求每个零件被生产需要多少天(保证在3到10天) 解题思路:因为不能确定每个工作日程具体生产了几天,所以对应列出的方程均为线性模方程(模7),所以在高斯消元的过程中遇到除法要转换成乘上逆元. #include <cstdio> #include <cs

UVA 10679 I love Strings!!!(AC自动机)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1620 题意: 给出一个文本串和若干个模式串,问模式串是否在文本串中出现过. 分析: 简单粗暴的AC自动机模板题,要注意模式串可能有重复的情况. /* * * Author : fcbruce * * Time : Sat 04 Oct 2014 03:30:15 PM CST * */ #i

UVA 1564 - Widget Factory(高斯消元)

UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间,每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每个零件的制作时间,还要判断一下多解和无解的情况 思路:对于每段时间列出一个方程,这样一共列出m个方程解n个变元,利用高斯消元去求解,注意每个方程都是MOD 7的,所以在高斯消元过程中遇到除法要求该数字%7的逆元去进行运算 代码: #include <cstdio> #include <cstring> #i

UVa 10152 - ShellSort 题解

按他的方法排序,每次移动一个数到顶点,排成需要的序列. Problem D: ShellSort He made each turtle stand on another one's back And he piled them all up in a nine-turtle stack. And then Yertle climbed up. He sat down on the pile. What a wonderful view! He could see 'most a mile! T