zoukankan      html  css  js  c++  java
  • UVaLive 4452 The Ministers' Major Mess (TwoSat)

    题意:有 m 个人对 n 个方案投票,每个人最多只能对其中的4个方案投票(其他的相当于弃权),每一票要么支持要么反对。问是否存在一个最终决定,使得每个投票人都有超过一半的建议被采纳,在所有可能的最终决定中,哪些方案的态度是确定的。

    析:注意这个题是超过一半,是TwoSat 算法,对于投小于三票的,他的票必须都成立(因为要超过一半),同理大于两票的最多一票不成立,这样考虑就是twosat的问题了,也就是说对于小于三票的,那么就相当于是标记了必须成立,对于大于两票的,那么就是如果有一个不成立,那么其他的必须全成立,那么就全连上边。

    代码如下:

    #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 = 100 + 10;
    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;
    }
    
    char ans[maxn];
    
    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);
      }
    
      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;
            bool ok1 = dfs(i);
            while(c)  mark[S[--c]] = 0;
            bool ok2 = dfs(i^1);
            while(c)  mark[S[--c]] = 0;
            if(ok1 && ok2)  ans[i>>1] = '?';
            else if(ok1)  ans[i>>1] = 'n';
            else if(ok2)  ans[i>>1] = 'y';
            else  return false;
          }
        ans[n] = 0;
        return true;
      }
    };
    TwoSat twosat;
    
    int a[10], val[10];
    
    int main(){
      int kase = 0;
      while(scanf("%d %d", &n, &m ) == 2 && n+m){
        twosat.init(n);
        for(int i = 0; i < m; ++i){
          int num;  scanf("%d", &num);
          for(int j = 0; j < num; ++j){
            char ch;
            scanf("%d %c", a+j, &ch);
            --a[j];
            val[j] = ch == 'y';
          }
          if(num < 3)  for(int j = 0; j < num; ++j)
            twosat.add_clause(a[j], val[j], a[j], val[j]);
          else  for(int j = 0; j < num; ++j)
            for(int k = j+1; k < num; ++k)
              twosat.add_clause(a[j], val[j], a[k], val[k]);
        }
        printf("Case %d: ", ++kase);
        if(!twosat.solve())  puts("impossible");
        else puts(ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    Mybatis传入list的注意点
    synchronized锁
    手撸红黑树
    Vue cdn加速配置
    多线程之BlockingQueue中 take、offer、put、add的一些比较
    线程池
    ThreadLocal
    jdk LocalDateTime mybatis 空指针解决办法
    不同对象中 类型与属性名的属性 进行数据转换
    HashMap 的put方法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7739280.html
Copyright © 2011-2022 走看看