zoukankan      html  css  js  c++  java
  • UVALive3713-Astronauts(2-SAT)

    题目链接


    题意:有A、B、C3个任务分配给n个宇航员,当中每一个宇航员恰好分配一个任务。如果n个宇航员的平均年龄为x,仅仅有年龄大于x的才干领取A任务;仅仅有年龄严格小于x的才干领取B任务,而任务C没有限制。有m对宇航员相互讨厌,因此不能分配同一任务。

    求出能否找出符合的任务方案。

    思路:用xi表示第i个宇航员的分配方案。

    年龄大于等于x的能够选择A(xi = true)和C(xi+1 = false),年龄小雨x的能够选择B(xi = true)和C(xi+1 = false)。考虑一对互相讨厌的宇航员的话,当不属于同一类时。能够为xi V xj,即两个之中要有一个为真;当属于同一类时,要用两个语句表示xi V xj、~xi V ~xj。即前者表示一个为true,后者表示一个为false。

    代码:

    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 100005;
    
    struct TwoSAT {
        int n;
        vector<int> G[maxn*2];
        bool mark[maxn*2];
        int S[maxn*2], 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].size(); i++)
                if (!dfs(G[x][i])) return false;
            return true;
        }
    
        void init(int n) {
            this->n = n;
            for (int i = 0; i < n*2; i++) 
                G[i].clear();
            memset(mark, 0, sizeof(mark));
        }
        
        //x = xval or y = yval
        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 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;
        }
    };
    
    TwoSAT solver;
    
    int n, m, total_age, age[maxn];
    
    int is_young(int x) {
        return age[x] * n < total_age;
    }
    
    int main() {
        while(scanf("%d%d", &n, &m) == 2 && n) {
            total_age = 0;
            for(int i = 0; i < n; i++) { 
                scanf("%d", &age[i]); 
                total_age += age[i]; 
            }
    
            solver.init(n);
            for(int i = 0; i < m; i++) {
                int a, b;
                scanf("%d%d", &a, &b); 
                a--; 
                b--;
                if(a == b) continue;
                solver.add_clause(a, 1, b, 1);
                if(is_young(a) == is_young(b)) 
                    solver.add_clause(a, 0, b, 0); 
            }
    
            if(!solver.solve()) printf("No solution.
    ");
            else {
                for(int i = 0; i < n; i++)
                    if(solver.mark[i*2]) printf("C
    "); 
                    else if(is_young(i)) printf("B
    "); 
                    else printf("A
    "); 
            }
        }
        return 0;
    }


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    BUUCTF-web ZJCTF,不过如此
    BUUCTF-web ikun(Python 反序列化)
    BUUCTF-web web1 (无列名注入)
    求前 n 个正整数的 k 次方和
    增根和失根:解方程的逻辑问题
    斜二测画法的经验公式
    像素转换mm
    正则表达式收集
    (转)mssql sp_addextendedproperty 用法,作用
    (转).net平台下垃圾回收机制
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4905584.html
Copyright © 2011-2022 走看看