LA 3713 - Astronauts 2-SAT

本文介绍了一种用于为不同级别太空任务分配宇航员的算法。该算法考虑了宇航员之间的相互关系及年龄因素,确保高级任务由资深宇航员承担,而年轻宇航员则被指派到较次要的任务中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 then 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$ij$ \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
-------------

白书的2-SAT模板效率还是很高的嘛 

-------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn=111111;
const int maxm=111111;

struct TWO_SAT{
    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;

int year[maxn];

int main()
{
    int n,m;
    int x,y;
    int avg;
    while (~scanf("%d%d",&n,&m))
    {
        if (n==0&&m==0) break;
        TwoSAT.init(n);
        avg=0;
        for (int i=0; i<n; i++)
        {
            scanf("%d",&year[i]);
            avg+=year[i];
        }
        avg/=n;
        while (m--)
        {
            scanf("%d%d",&x,&y);
            x--;
            y--;
            if (x==y) continue;
            if ( (year[x]>=avg)==(year[y]>=avg) )
            {
                TwoSAT.add_clause(x,1,y,1);
                TwoSAT.add_clause(x,0,y,0);
            }
            else
            {
                TwoSAT.add_clause(x,1,y,1);
            }
        }
        if (!TwoSAT.solve())
        {
            puts("No solution.");
        }
        else
        {
            for (int i=0;i<n;i++)
            {
                if (!TwoSAT.mark[i*2])
                {
                    if (year[i]>=avg) puts("A");
                    else puts("B");
                }
                else
                {
                    puts("C");
                }
            }
        }
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值