LA 3713

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 1n100000 <tex2html_verbatim_mark>and 1m100000 <tex2html_verbatim_mark>. The
number n <tex2html_verbatim_mark>is the number
of astronauts. The next n <tex2html_verbatim_mark>lines specify
the age of the n<tex2html_verbatim_mark>astronauts; each
line contains a single integer number between 0 and 200. The next m <tex2html_verbatim_mark>lines contains
two integers each, separated by a space. A line containing i <tex2html_verbatim_mark>and j <tex2html_verbatim_mark>(1ijn) <tex2html_verbatim_mark>means that
the i <tex2html_verbatim_mark>-th astronaut
and the j <tex2html_verbatim_mark>-th astronaut
hate each other.

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

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 <tex2html_verbatim_mark>-th line
describes which mission the i <tex2html_verbatim_mark>-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.

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #include <stack>
6 #include <vector>
7
8 using namespace std;
9
10 const int MAX_N = 1e5 + 5;
11 int N,M;
12 int low[MAX_N * 2],pre[MAX_N * 2],cmp[MAX_N * 2];
13 int age[MAX_N];
14 vector <int> G[2 * MAX_N];
15 stack <int> S;
16 int dfs_clock,scc_cnt;
17
18 void dfs(int u) {
19 pre[u] = low[u] = ++dfs_clock;
20 S.push(u);
21 for(int i = 0; i < G[u].size(); ++i) {
22 int v = G[u][i];
23 if(!pre[v]) {
24 dfs(v);
25 low[u] = min(low[u],low[v]);
26 } else if(!cmp[v]) {
27 low[u] = min(low[u],pre[v]);
28 }
29 }
30
31 if(pre[u] == low[u]) {
32 ++scc_cnt;
33 for(;;) {
34 int x = S.top(); S.pop();
35 cmp[x] = scc_cnt;
36 if(x == u) break;
37 }
38 }
39 }
40
41 bool scc() {
42 dfs_clock = scc_cnt = 0;
43 memset(cmp,0,sizeof(cmp));
44 memset(pre,0,sizeof(pre));
45
46 for(int i = 1; i <= 2 * N; ++i) {
47 if(!pre[i]) dfs(i);
48 }
49
50 for(int i = 1; i <= N; ++i) {
51 if(cmp[i] == cmp[N + i]) return false;
52 }
53
54 return true;
55 }
56 int main()
57 {
58 freopen("sw.in","r",stdin);
59 while(~scanf("%d%d",&N,&M) && (N || M)) {
60 int sum = 0;
61 for(int i = 1; i <= N; ++i) {
62 scanf("%d",&age[i]);
63 sum += age[i];
64 }
65 for(int i = 1; i <= 2 * N; ++i) G[i].clear();
66
67 for(int i = 1; i <= M; ++i) {
68 int u,v;
69 scanf("%d%d",&u,&v);
70 if(age[u] * N >= sum && age[v] * N < sum
71 || age[u] * N < sum && age[v] * N >= sum) {
72 G[v + N].push_back(u);
73 G[u + N].push_back(v);
74 } else {
75
76 G[u].push_back(v + N);
77 G[v].push_back(u + N);
78 G[v + N].push_back(u);
79 G[u + N].push_back(v);
80 }
81
82 }
83
84 if(!scc()) {
85 printf("No solution.\n");
86 } else {
87 for(int i = 1; i <= N; ++i) {
88 if(age[i] * N >= sum) {
89 printf("%c\n",cmp[i] < cmp[i + N] ? ‘A‘ : ‘C‘);
90 } else {
91 printf("%c\n",cmp[i] < cmp[i + N] ? ‘B‘ : ‘C‘);
92 }
93 }
94 }
95 }
96
97 return 0;
98 }



LA 3713,布布扣,bubuko.com

时间: 2025-01-03 15:45:48

LA 3713的相关文章

Uva 1391 (LA 3713) Astronauts (2-SAT问题)

今天学了2-SAT问题,就找了这道例题,敲了一下,还好过了. 2-SAT问题应该是把一些布尔变量之间的逻辑关系反映到一个无向图(有时可能是有向图)上来.通过推导的形式在这个有向图里面补边.再通过确定一些变量的值,使所有的变量都能符合题意中逻辑关系.补边方式 即如果Xi = true && Xj = false 不符合题意,那么如果Xi == true ,那么就在 Xi = true 和 Xj = true之间连一条边, 使得当Xi = true 时能马上确定 Xj = true. 在实际操

LA 3713 Astronauts

给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1714 显然每个人只有两种选择:C或者A/B.而且后一种只和这个人的年龄有关. 这就是一个很显然的2-SAT模型了,显然有矛盾的不能都选C,如果两个人的年龄段还一样,那就更悲剧了,也不能同时选A/B. #include<iostream> #incl

2-SAT 问题与解法小结

2-SAT 问题与解法小结 这个算法十分的奇妙qwq... 将一类判定问题转换为图论问题,然后就很容易解决了. 本文有一些地方摘录了一下赵爽<2-SAT解法浅析> (侵删) 一些概念: \(SAT\)问题:就是给一些布尔变量赋值,使得所有给你的条件成立的问题---适定性(Satisfiability)问题.我们令\(k\)为所有条件中含有变量的最大值,那么我们就可以称其为\(k-SAT\)问题. 可以证明\(k>2\)时候为NP完全问题,而\(k=2\)的时候存在多项式解法. \(2-S

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

LA 3942 Remember the Word (Trie)

Remember the Word 题目:链接 题意:给出一个有S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法? 思路:令d[i]表示从字符i开始的字符串(后缀s[i..L])的分解数,这d[i] = sum{d(i+len(x)) | 单词x是其前缀}.然后将所有单词建成一个Trie树,就可以将搜索单词的复杂度降低. 代码: #include<map> #include<set> #include<queue>

LA 2678 Subsequence

有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn). 这里二分查找用到库函数lower_bound() 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #inclu

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 2031

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his