zoukankan      html  css  js  c++  java
  • uva 1391 2-SAT

    The Bandulu Space Agency (BSA) has plans for the following three space missions:

    • Mission A: Landing on Ganymede, the largest moon of Jupiter.
    • Mission B: Landing on Callisto, the second largest moon of Jupiter.
    • Mission C: Landing on Titan, the largest moon of Saturn.

    Your task is to assign a crew for each mission. BSA has trained a number of excellent astronauts; everyone of them can be assigned to any mission. However, if two astronauts hate each other, then it is not wise to put them on the same mission. Furthermore, Mission A is clearly more prestigious than Mission B; who would like to go to the second largest moon if there is also a mission to the largest one? Therefore, the assignments have to be done in such a way that only young, inexperienced astronauts go to Mission B, and only senior astronauts are assigned to Mission A. An astronaut is considered young if their age is less than the average age of the astronauts and an astronaut is senior if their age is at least the averageage. Every astronaut can be assigned to Mission C, regardless of their age (but you must not assign two astronauts to the same mission if they hate each other).

    Input 

    The input contains several blocks of test cases. Each case begins with a line containing two integers 1$ le$n$ le$100000 and 1$ le$m$ le$100000 . The number n is the number of astronauts. The next n lines specify the age of the n astronauts; each line contains a single integer number between 0 and 200. The next m lines contains two integers each, separated by a space. A line containing i and j (1$ le$i, j$ le$n) means that the i -th astronaut and the j -th astronaut hate each other.

    The input is terminated by a block with n = m = 0 .

    Output 

    For each test case, you have to output n lines, each containing a single letter. This letter is either `A', `B', or `C'. The i -th line describes which mission the i -th astronaut is assigned to. Astronauts that hate each other should not be assigned to the same mission, only young astronauts should be assigned to Mission B and only senior astronauts should be assigned to Mission A. If there is no such assignment, then output the single line `No solution.' (without quotes).

    Sample Input 

    16 20
    21
    22
    23
    24
    25
    26
    27
    28
    101
    102
    103
    104
    105
    106
    107
    108
    1 2
    3 4
    5 6 
    7 8
    9 10
    11 12
    13 14
    15 16
    1 10
    2 9
    3 12
    4 11
    5 14
    6 13 
    7 16
    8 15
    1 12
    1 13
    3 16
    6 15
    0 0
    

    Sample Output 

    B
    C
    C
    B
    C
    B
    C
    B
    A
    C
    C
    A
    C
    A
    C
    A

    lrj例题,每个宇航员恰有两种选择,这样可用布尔变量表示。
    约束条件: 任意两个宇航员要么处于同年龄段,比如<average,要么不同; 相同时,需要加两个子句,避免相互讨厌者选同一目的地; 不同时避免选择C目的地。
    Submission Error:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define MAXN 111111
    vector<int> g[MAXN<<1];
    bool mark[MAXN<<1];
    int s[MAXN<<1], c, age[MAXN];
    
    inline bool same(int u, int v, int ave){                //是否在同一年龄段
        if(age[u]<ave && age[v]<ave) return true;
        if(age[u]>=ave && age[v]>=ave) return true;
        return false;
    }
    
    inline void add(int u, int a, int v, int b){            //加边
        u = (u<<1) + a;
        v = (v<<1) + b;
        g[u].push_back(v^1);
        g[v].push_back(u^1);
    }
    
    bool dfs(int u){
        if(mark[u]) return true;                        //如果已标记过
        if(mark[u^1]) return false;                     //如果否命题假设为真,则矛盾
        mark[u]=true;       s[c++]=u;
        int i, tl=g[u].size();
    
        for(i=0; i<tl; i++)
            if(!dfs(g[u][i])) return false;
        return true;
    }
    
    void solve(int n, int ave){
        int i,j;
        memset(mark, 0, sizeof(mark));
        for(i=0; i<n<<1; i+=2) if(!mark[i] && !mark[i+1]){
            c=0;
            if(!dfs(i)){                        //标记i
                while(c--) mark[s[c]]=false;
                if(!dfs(i+1)){                  //标记否命题
                    printf("No solution.
    ");
                    return ;
                }
            }
        }
    
        char ch, ta;
        for(i=0; i<n<<1; i+=2){
            ta=age[i>>1];
            if(!mark[i]) ch='C';
            else ch=(ta<ave ? 'B' : 'A');
            putchar(ch);        puts("");
        }
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        //freopen("C:\Users\Administrator\Desktop\out.txt","w",stdout);
        int n,m;
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            int i, ave=0, j;
            for(i=0; i<n; i++){
                scanf(" %d", &age[i]);
                ave+=age[i];
            }
            int u, v;
            ave=ceil(1.0*ave/n);
    
            for(i=0; i<n<<1; i++)   g[i].clear();
            for(i=0; i<m; i++){
                scanf(" %d %d", &u, &v);    u--;    v--;
                if(same(u, v, ave)){
                    add(u, 0, v, 0);    add(u, 1, v, 1);
                }
                else add(u, 1, v, 1);
            }
            solve(n, ave);
        }
        return 0;
    }
    

  • 相关阅读:
    ISAPI实现静态页面后并用c#实现分页
    aspx里构造函数里无法使用session,需要重写一个方法放在load里面就能正常使用session了
    记录学习MVC过程,MVC异步请求(五)
    记录学习MVC过程,MVC验证(四)
    记录学习MVC过程,MVC简单路由(三)
    【读书笔记】【韭菜的自我修养】
    【中间件】redis的学习
    【java基础】线程池
    【算法】leetcode刷题 腾讯精选50题 一
    【碎语】让你废掉的七个行为
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3311435.html
Copyright © 2011-2022 走看看