UVALive3713-Astronauts (2-Sat)

Source: Internet
Author: User

Question Link


Task A, Task B, and task C3 are assigned to N astronauts, and each of them is assigned a task. Assume that the average age of N astronauts is X. Only those older than X can receive task a. Only those younger than X can receive Task B. Task C is not limited. M hates each other and therefore cannot assign the same task. Determine whether the task plan can be found.

Idea: Use XI to represent the allocation plan of the I-th astronaut. If the age is greater than or equal to X, you can select a (xi = true) and C (XI + 1 = false). If the age is light rain X, you can select B (xi = true) and C (XI + 1 = false ). Consider an annoying pair of Astronauts. If they do not belong to the same category, they can be Xi v XJ, that is, either of them must be true. If they belong to the same category, use two statements to represent Xi v XJ and ,~ Xi v ~ XJ, that is, the former indicates true, and the latter indicates false.

Code:

#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.\n");        else {            for(int i = 0; i < n; i++)                if(solver.mark[i*2]) printf("C\n");                 else if(is_young(i)) printf("B\n");                 else printf("A\n");         }    }    return 0;}


UVALive3713-Astronauts (2-Sat)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.