# Algorithm: UVA 1391 Astronauts (2-sat + output scheme)

Source: Internet
Author: User
Tags bool int size min

"The main effect of the topic"

There are n astronauts who, by age, are younger than the average age, and older than the average age are experienced astronauts.

Now to allocate them to a,b,c three space stations, where a station only experienced astronauts to go, and B station is only young to go, C station can go.

M is a nuisance to the astronauts and cannot allow them to work on the same space station.

The output of each astronaut should be assigned to which space station, if not the output no solution.

Ideas

For each astronaut, he has only two places to go: C or non-C (if Young is B, sophisticated is a), so it is two to choose one, you can think of 2-sat decision problem.

When the edge is built, it can be divided into good condition plus edge. And then there's the template set.

Code

```#include &lt;iostream&gt; #include &lt;queue&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #define WHITE-1

#define RED 1 #define BLUE 0 using namespace std;
const int INF = 0X3F3F3F3F;
const int MAXN = 100010;
const int VN = MAXN*2;

const int EN = VN*5;
int n, m;
int AGE[MAXN];

int sum;
struct graph{int size, HEAD[VN]; Struct{int u, V, next;
E[en];
void Addedge (int u, int v) {e[size].u = u;
E[SIZE].V = v;

}}g, G1;
Class tow_sat{Public:bool Check () {SCC ();
for (int i=0; i&lt;n; ++i) if (belong[i] = = Belong[i+n]) return false;
return true;
} void Toposort () {g1.init ();
memset (indeg, 0, sizeof (INDEG)); for (int i=0; i&lt;n; ++i) {Opp[beLong[i]] = belong[i+n];
Opp[belong[i+n]] = belong[i];
for (int e=0; e&lt;g.size; ++e) {int u = belong[g.e[e].u];
int v = belong[g.e[e].v];
if (u==v) continue;
++indeg[u];
} queue&lt;int&gt;que;
memset (color, white, sizeof (color));

for (int i=1; i&lt;=bcnt; ++i) if (!indeg[i)) Que.push (i);
while (!que.empty ()) {int u = que.front ();
Que.pop ();
if (Color[u]!= white) continue;
Color[u] = RED;
Color[opp[u]] = BLUE; for (int e=g1.head[u]; e!=-1; e=g1. E[e].next) {int v=g1.
E[E].V;
if (--indeg[v] = = 0) que.push (v); }//output for (int i=0; i&lt;n; ++i) {if (color[belong[i)] = = RED) {if (Age[i]*n &lt; sum) puts ("B");
Else puts ("A");
}else{puts ("C");
}} private:void Tarjan (const int u) {int V;
Dfn[u] = low[u] = ++idx;
Instack[u] = true;
sta[top++] = u;
for (int e=g.head[u]; E!=-1 e=g.e[e].next) {v = g.e[e].v;
if (dfn[v] = = 1) Tarjan (v), Low[u]=min (Low[u), low[v]);
else if (Instack[v]) low[u]=min (Low[u], dfn[v]);
} if (low[u] = = Dfn[u]) {++bcnt;
do{V=sta[--top];
INSTACK[V] = false;
BELONG[V] = bcnt;
}while (U!= v);
} void SCC () {idx = bcnt = top = 0;
memset (instack, 0, sizeof (instack));
memset (DFN,-1, sizeof (DFN));
for (int i=0; i&lt;2*n; ++i) if (dfn[i] = = 1)                Tarjan (i);
} private:int idx, top, bcnt;
int LOW[VN],DFN[VN],BELONG[VN],STA[VN];
BOOL INSTACK[VN];
int COLOR[VN], indeg[vn], OPP[VN];

}sat;
int main () {while (~scanf ("%d%d", &amp;n, &amp;m) &amp;&amp; n+m) {sum = 0;
for (int i=0; i&lt;n; ++i) {scanf ("%d", &amp;age[i]);

Sum + + age[i];
} g.init ();
int A, B;
for (int i=0; i&lt;m; ++i) {scanf ("%d%d", &amp;a, &amp;b); --a;
--b;
if (Age[a]*n &lt; sum &amp;&amp; age[b]*n &lt; sum) {//small, small G.addedge (A, b+n);
}else if (Age[a]*n &lt; sum &amp;&amp; age[b]*n &gt;= sum) {//small, large G.addedge (a+n, b);
G.addedge (B+n, a); }else if (age[a]*n &gt;= sum &amp;&amp;
Age[b]*n &lt; sum) {//large, small G.addedge (a+n, b);
} if (!sat.check ()) puts ("No solution.");
else Sat.toposort ();
return 0; }```

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Related Keywords:

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.