Ultraviolet A 1391-Astronauts (2-SET)
Ultraviolet A 1391-Astronauts
Question Link
Given some astronauts who are younger than the average age can do both A and C, and are greater than or equal to B and C, they now know that some astronauts hate each other and cannot perform the same task, always try to meet the conditions
Train of Thought: 2set problem. If the two astronauts are of the same type, the two astronauts will do the same, adding a real or real side and a fake side. If the type is different, add a real or real edge.
Code:
#include
#include
#include
#include
#include using namespace std;const int MAXNODE = 100005;struct TwoSet {int n;vector
g[MAXNODE * 2];bool mark[MAXNODE * 2];int S[MAXNODE * 2], sn;void init(int tot) {n = tot * 2;for (int i = 0; i < n; i += 2) {g[i].clear();g[i^1].clear();}memset(mark, false, sizeof(mark));}void add_Edge(int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].push_back(v);g[v^1].push_back(u);}bool dfs(int u) {if (mark[u^1]) return false;if (mark[u]) return true;mark[u] = true;S[sn++] = u;for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (!dfs(v)) return false;}return true;}bool solve() {for (int i = 0; i < n; i += 2) {if (!mark[i] && !mark[i + 1]) {sn = 0;if (!dfs(i)){for (int j = 0; j < sn; j++)mark[S[j]] = false;sn = 0;if (!dfs(i + 1)) return false;}}}return true;}} gao;const int N = 100005;int n, m, age[N], sum;vector
g[N];int main() {while (~scanf("%d%d", &n, &m) && n) {sum = 0;gao.init(n);for (int i = 0; i < n; i++) {scanf("%d", &age[i]);g[i].clear();sum += age[i];}for (int i = 0; i < n; i++) {if (age[i] * n < sum)age[i] = 0;elseage[i] = 1;}int u, v;while (m--) {scanf("%d%d", &u, &v);u--; v--;g[u].push_back(v);}for (int u = 0; u < n; u++) {for (int j = 0; j < g[u].size(); j++) {int v = g[u][j];if (age[u]^age[v])gao.add_Edge(u, 1, v, 1);else {gao.add_Edge(u, 0, v, 0);gao.add_Edge(u, 1, v, 1);}}}if (gao.solve()) {for (int i = 0; i < n; i++) {if (age[i] && gao.mark[i * 2 + 1]) printf("A\n");else if (age[i] == 0 && gao.mark[i * 2 + 1]) printf("B\n");else printf("C\n");}} else printf("No solution.\n");}return 0;}