[UVA]1391 Astronauts 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 ≤ n ≤ 100000 and 1 ≤ m ≤ 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 ≤ i, j ≤ 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

题解

  题意:给出一些宇航员他们的年龄,x是他们的平均年龄,其中A任务只能给年龄大于等于x的人,B任务只能给小于x的人,C任务没有限制。再给出m对人,他们不能同任务。现在要你输出一组符合要求的任务安排。
  以前做过这道题, 但当初完全是套的蓝书上的搜索的模板, 并没有很好地运用对称性的性质, 如今重学2-Sat会了拓扑的做法, 重新来写也算是朝花夕拾了吧…. 发现这种逻辑转化图论问题等理论深化内容十分的好玩…
  如果u, v互相冲突, 并且年龄相同, 那么很显然u选(A, B), v就只能选C, 反之同理. 那么设节点u为u选(A, B), u’为选C. 那么u就像v’连边, v向u’连边.
  同时如果冲突, 那么u选C显然v不能选C, 那么无论年龄相不相同u’得向v连边(代表u选C, v就只能选(A, b)), v’得向u连边.
  然后tarjan缩点成DAG, 顺便判无解. 然后跑一边拓扑即可得解. 注意int和double的比较可能有误差, 最好是int和int的比较.

#include<bits/stdc++.h>
#define clear(a) memset(a, 0, sizeof(a))
using namespace std;
queue<int> q;
const int maxn = 2e6 + 5;
bool flag;
int n, m, nn, num, cnt, top, idx, sum;
int st[maxn], ed[maxn], low[maxn], in[maxn], dfn[maxn];
int s[maxn], age[maxn], rev[maxn], vis[maxn], h[maxn], scc[maxn];
inline const int read() {
    register int x = 0;
    register char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x;
}
struct edge { int nxt, v; }e[maxn << 1];
inline void init() {
    flag = false;
    nn = num = top = idx = cnt = sum = 0;
    clear(in), clear(dfn);
    clear(s),  clear(vis), clear(h), clear(scc);
}
inline void add(int u, int v, int opt) {
    e[++ num].v = v, e[num].nxt = h[u], h[u] = num;
    if (opt) st[num] = u, ed[num] = v;
}
void dfs(int u) {
    s[++ top] = u;
    low[u] = dfn[u] = ++ idx;
    for (int i = h[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        if (!dfn[v]) 
            dfs(v), low[u] = min(low[u], low[v]);
        else if (!scc[v]) low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u]) {
        ++ cnt;
        while (s[top + 1] != u) 
            scc[s[top --]] = cnt;
    }
}
void tp() {
    for (int i = 1; i <= cnt; ++ i)
        if (!in[i]) q.push(i);
    while (!q.empty()) {
        int u = q.front();q.pop();
        if (vis[u]) continue;
        vis[u] = 2, vis[rev[u]] = 1;
        for (int i = h[u]; i; i = e[i].nxt)
            if (!(-- in[e[i].v]))
                q.push(e[i].v);
    }
}
int main() {
    register int i;
    while (n = read() , m = read(), n || m) {
        init();
        for (i = 1; i <= n; ++ i) age[i] = read(), sum += age[i];
        for (i = 1; i <= m; ++ i) {
            int u = read(), v = read();
            if (u == v) continue;
            add(2 * u + 1, 2 * v, 1), add(2 * v + 1, 2 * u, 1);
            if (age[u] * n < sum && age[v] * n < sum || 
                age[u] * n >= sum && age[v] * n >= sum)
            add(2 * u, 2 * v + 1, 1), add(2 * v, 2 * u + 1, 1);
        }
        for (i = 2; i <= 2 * n + 1; ++ i)
            if (!dfn[i]) dfs(i);
        for (i = 1; i <= n; ++ i)
            if (scc[2 * i] == scc[2 * i + 1]) {
                flag = true;
                puts("No Solution");
                break;
            }
        if (flag) continue;
        clear(h), nn = num, num = 0;
        for (i = 1; i <= nn; ++ i)
            if (scc[st[i]] != scc[ed[i]])
                add(scc[st[i]], scc[ed[i]], 0), in[scc[ed[i]]] ++;
        for (i = 1; i <= n; ++ i)
            rev[scc[2 * i]] = scc[2 * i + 1], rev[scc[2 * i + 1]] = scc[2 * i];
        tp();
        for (i = 1; i <= n; ++ i) {
            if (vis[scc[2 * i]] == 2) puts("C");
            else if (age[i] * n < sum) puts("B");
            else puts("A");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hello, today I want to introduce one of my favorite movies, Interstellar. Interstellar is a science-fiction drama film directed by Christopher Nolan, released in 2014. The movie takes place in a dystopian future where a blight caused by environmental changes has wiped out most of Earth's crops, making farming nearly impossible. Cooper, a former astronaut, is recruited by a secret NASA project to find a new home for the human race on another planet, as Earth is becoming uninhabitable. Cooper, played by actor Matthew McConaughey, leads a group of other astronauts into space, travelling through a wormhole to another galaxy in the hope of finding a suitable planet to populate. Along the way, they encounter several obstacles including physical dangers, time dilation, and the potential of never returning home. The movie explores complex scientific concepts such as the theory of relativity and the nature of time, while also examining the emotional toll that space travel has on individuals and family relationships. The soundtrack, composed by Hans Zimmer, further enhances the emotional impact of the film. Interstellar received critical acclaim for its innovative storytelling, stunning visual effects, and thought-provoking themes. The movie also earned five Oscar nominations and won the award for Best Visual Effects. Overall, Interstellar is an extraordinary film that pushes the boundaries of science-fiction and delivers a powerful narrative about humanity's search for a new home in the universe.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值