zoukankan      html  css  js  c++  java
  • UVa 11294 Wedding (TwoSat)

    题意:有 n-1 对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧。由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人。任意一对夫妻不能坐在桌子的同侧,另外有m对人吵过架,而新娘不希望看到两个吵过架的人坐在他的对面,问如何安排这些座位。

    析:很明显的TwoSat问题,假设mark[i<<1]标记了,就是和新娘同侧,否则就是和新娘对侧,这样的话,对于每对吵架的人,他们要么在不同的侧,要么在新娘同侧,注意一定要注意要提前把新娘标记了,而且题目有可能是新娘或者新郎吵过架,RE了好多次,还以为是数组开小了。。。

    代码如下:

    #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>
    #include <numeric>
    #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<LL, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 60 + 100;
    const int maxm = 1e6 + 5;
    const int mod = 10007;
    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;
    
      void init(int n){
        this-> n = n;
        for(int i = 0; i < (n<<1); ++i)  G[i].cl;
        ms(mark, 0);  mark[0] = 1;
      }
    
      void add_clause(int x, int xval, int y, int yval){
        x = x << 1 | xval;
        y = y << 1 | yval;
        G[x^1].pb(y);
        G[y^1].pb(x);
      }
    
      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;
      }
    
      bool solve(){
        for(int i = 0; i < (n<<1); i += 2)
          if(!mark[i] && !mark[i^1]){
            c = 0;
            if(!dfs(i)){
              while(c > 0)  mark[S[--c]] = 0;
              if(!dfs(i^1))  return false;
            }
          }
        return true;
      }
    };
    TwoSat twosat;
    
    int main(){
      while(scanf("%d %d", &n, &m) == 2 && n+m){
        twosat.init(n);
        int x, y;
        char c1, c2;
        while(m--){
          scanf("%d%c %d%c", &x, &c1, &y, &c2);
          twosat.add_clause(x, c1 == 'h', y, c2 == 'h');
        }
        if(!twosat.solve()){ puts("bad luck");  continue;  }
        for(int i = 1; i < n; ++i)
          printf("%d%c%c", i, "hw"[twosat.mark[i<<1]], " 
    "[i+1==n]);
      }
      return 0;
    }
    

      

  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7732372.html
Copyright © 2011-2022 走看看