zoukankan      html  css  js  c++  java
  • Gym 101201F Illumination (Two-Sat)

    题意:一个n*n的房子,有很多灯,每个格子只能被上下方向照一次、左右方向照一次,每个灯可以选择上下或是左右照,照明长度以自身位置为中心,占用2*r+1个格子。问能否安排一种方案,使所有格子满足条件。

    析:典型的Two-Sat,对于行来说,如果两个能够交叉,那么他们不能都是左右,对于列也是一样。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e4 + 50;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    
    struct TwoSat{
      int n;
      vector<int> G[maxn<<1];
      bool mark[maxn<<1];
      int S[maxn<<1], c;
    
      bool dfs(int x){
        if(mark[x^1])  return false;
        if(mark[x])  return true;
        mark[x] = true;
        S[c++] = x;
        for(int i = 0; i < G[x].sz; ++i)
          if(!dfs(G[x][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].pb(y);
        G[y^1].pb(x);
      }
    
      bool solve(){
        for(int i = 0; i < n*2; i += 2){
          if(!mark[i] && !mark[i+1]){
            c = 0;
            if(!dfs(i)){
              while(c > 0)  mark[S[--c]] = false;
              if(!dfs(i+1))  return false;
            }
          }
        }
        return true;
      }
    };
    map<P, int> mp;
    vector<int> row[1005], col[1005];
    TwoSat twosat;
    
    int main(){
      int r, l;
      scanf("%d %d %d", &n, &r, &l);
      int cnt = 0;
      for(int i = 0; i < l; ++i){
        int x, y;
        scanf("%d %d", &x, &y);
        --x;  --y;
        mp[P(x, y)] = cnt++;
        row[x].pb(y);
        col[y].pb(x);
      }
      for(int i = 0; i < n; ++i){
        sort(row[i].begin(), row[i].end());
        sort(col[i].begin(), col[i].end());
      }
      twosat.n = cnt;
      for(int i = 0; i < n; ++i){
        for(int j = 0; j < row[i].sz; ++j){
          int k = j + 1;
          while(k < row[i].sz && row[i][j] + r >= row[i][k] - r){
            twosat.add_clause(mp[P(i, row[i][j])], 0, mp[P(i, row[i][k])], 0);
            ++k;
          }
        }
        for(int j = 0; j < col[i].sz; ++j){
          int k = j + 1;
          while(k < col[i].sz && col[i][j] + r >= col[i][k] - r){
            twosat.add_clause(mp[P(col[i][j], i)], 1, mp[P(col[i][k], i)], 1);
            ++k;
          }
        }
      }
    
      puts(twosat.solve() ? "YES" : "NO");
      return 0;
    }
    

      

  • 相关阅读:
    第一轮 J
    第一轮 M
    第一轮 L
    第一轮 K
    第一轮 I
    第一轮 H
    第一轮 F
    第一轮 E
    第一轮 C
    12杭州online E 模拟
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7580362.html
Copyright © 2011-2022 走看看