1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 200006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; int A[MAXN];
vi G[MAXN] , Gr[MAXN]; int deg[MAXN] , td[MAXN]; int dw[MAXN][2] , sw[MAXN][2]; queue<pii> Q;
void solve() { cin >> n >> m; rep( i , 1 , m ) { int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , Gr[v].pb( u ); ++ deg[u]; } rep( i , 1 , n ) { if( !deg[i] ) { dw[i][0] = dw[i][1] = 1; Q.emplace( i , 0 ) , Q.emplace( i , 1 ); } td[i] = deg[i]; } while( !Q.empty() ) { int u = Q.front().fi , w = Q.front().se; Q.pop(); for( int v : Gr[u] ) { if( w == 0 && !dw[v][1] ) { dw[v][1] = 1; Q.emplace( v , 1 ); } else if( w == 1 ) { -- td[v]; if( !td[v] ) Q.emplace( v , 0 ) , dw[v][0] = 1; } } } rep( i , 1 , n ) { if( !deg[i] ) { sw[i][0] = 1; Q.emplace( i , 0 ); } if( !dw[i][0] ) sw[i][0] = -1; if( !dw[i][1] ) sw[i][1] = -1; td[i] = deg[i]; } while( !Q.empty() ) { int u = Q.front().fi , w = Q.front().se; Q.pop(); for( int v : Gr[u] ) { if( w == 0 && !sw[v][1] ) { sw[v][1] = 1; Q.emplace( v , 1 ); } else if( w == 1 && !sw[v][0] ) { -- td[v]; if( !td[v] ) Q.emplace( v , 0 ) , sw[v][0] = 1; } } } rep( i , 1 , n ) { if( !dw[i][0] ) putchar('D'); else if( !sw[i][0] ) putchar('W'); else putchar('L'); } puts(""); rep( i , 1 , n ) { if( sw[i][1] > 0 ) putchar('W'); else if( dw[i][1] ) putchar('L'); else putchar('D'); } puts(""); }
signed main() { freopen("game.in","r",stdin) , freopen("game.out","w",stdout);
solve(); }
|