Question:
There are N astronauts logging on to the planet .. astronauts of an average age greater than or equal to the average age are old astronauts... and then the little astronaut... old astronauts can log on to Planet A and planet C .. little astronauts can log on to Planet B, planet C .. there are some contradictions between astronauts .. you cannot log on to the same planet... please find any feasible login solution...
Question:
Because both the old astronauts and the little astronauts have a total of C... first, they are regarded as two States. They log on to C or do not log on to C. Therefore, for a hostile relationship <a, B>
1. When a logs on to c, B cannot log on to c... B, and a cannot log on to c... so that two directed edges can be constructed.
2. When a and B are of the same age .. so when a does not log on to c .. B must log on to c... B Does not log on to c .. a must log on to c... in this way, two directed edges are constructed under the condition...
Then run tarjan .. determine the feasibility and scale down .. then use toposort to find a set of feasible solutions... output .. when the old astronaut is not C, output .. little astronauts output B when not C .. regardless of the size .. c...
This question updates the toposort process .. at last, there were not such vulnerabilities and violence... the idea is to shrink the point and construct the reverse side... then Mark the point with the input degree 0 in the reverse Edge Graph (that is, the point with the output degree 0 in the source image )... marking process... mark it as "take ".. dye the points that are mutually exclusive to "not take "... this is very interesting .. for a scaled point .. I thought it would have multiple conflicting points .. however, practice has proved that it has only one conflicting point .. you just need to store the labels in an array...
Program:
# Include <iostream> # include <stdio. h> # include <cmath> # include <queue> # include <stack> # include <string. h> # include <map> # include <set> # include <algorithm> # define oo 1000000007 # define MAXN 100005 <1 # define MAXM 100000 <2 # define ll long longusing namespace std; struct node {int x, y, next;} line [MAXM]; int Lnum, _ next [MAXN], age [MAXN], dfn [MAXN], low [MAXN], tp [MAXN], tpnum, DfsIndex; int color [MAXN], d [MAXN], opp [MAXN]; bool Old [MAXN], instack [MAXN]; set <int> T [MAXN]; stack <int> mystack; queue <int> myqueue; void addline (int x, int y) {line [++ Lnum]. next = _ next [x], _ next [x] = Lnum; line [Lnum]. x = x, line [Lnum]. y = y;} void tarjan (int x) {instack [x] = true, mystack. push (x); dfn [x] = low [x] = ++ DfsIndex; for (int k = _ next [x]; k = line [k]. next) {int y = line [k]. y; if (! Dfn [y]) {tarjan (y); low [x] = min (low [x], low [y]);} else if (instack [y]) low [x] = min (low [x], dfn [y]);} if (low [x] = dfn [x]) {tpnum ++; do {x = mystack. top (); mystack. pop (); instack [x] = true; tp [x] = tpnum;} while (low [x]! = Dfn [x]) ;}} bool judge (int N) {for (int I = 0; I <N; I ++) if (tp [I <1] = tp [I <1 | 1]) return false; return true;} void toposort (int N) {int I, h; memset (color, 0, sizeof (color); while (! Myqueue. empty () myqueue. pop (); for (I = 1; I <= N; I ++) if (! D [I]) myqueue. push (I); while (! Myqueue. empty () {h = myqueue. front (); myqueue. pop (); if (color [h]) continue; color [h] = 1, color [opp [h] = 2; set <int >:: iterator it; for (it = T [h]. begin (); it! = T [h]. end (); it ++) {d [* it] --; if (! D [* it]) myqueue. push (* it) ;}} int main () {int N, M, I, sum, v; freopen ("input.txt", "r", stdin ); freopen ("output.txt", "w", stdout); while (~ Scanf ("% d", & N, & M) & (N | M) {sum = 0; for (I = 0; I <N; I ++) scanf ("% d", & age [I]), sum + = age [I]; Lnum = 0, memset (_ next, 0, sizeof (_ next); v = (int) (sum * 1.0/N); if (v * N <sum) v ++; memset (old, false, sizeof (old); for (I = 0; I <N; I ++) if (age [I]> = v) old [I] = true; while (M --) {int x, y; scanf ("% d", & x, & y); addline (x <1, y <1 | 1), addline (y <1, x <1 | 1); // x <1 select c... if (old [x] ^ old [y] = 0) addline (x <1 | 1, y <1), addli Ne (y <1 | 1, x <1);} memset (dfn, 0, sizeof (dfn); memset (instack, false, sizeof (instack )); while (! Mystack. empty () mystack. pop (); tpnum = DfsIndex = 0; for (I = 0; I <(N <1); I ++) if (! Dfn [I]) tarjan (I); if (! Judge (N) {printf ("No solution. \ n "); continue;} for (I = 1; I <= tpnum; I ++) T [I]. clear (); for (I = 0; I <(N <1); I ++) opp [tp [I] = tp [I ^ 1]; memset (d, 0, sizeof (d); for (I = 1; I <= Lnum; I ++) {int x = tp [line [I]. x], y = tp [line [I]. y]; if (x = y | T [y]. count (x) continue; T [y]. insert (x); d [x] ++;} toposort (tpnum); for (I = 0; I <N; I ++) {if (color [tp [I <1] = 1) printf ("C \ n"); else if (old [I]) printf ("A \ n"); else printf ("B \ n") ;}} return 0 ;}