1. 程式人生 > >Codeforces-936B Sleepy Game

Codeforces-936B Sleepy Game

space div com pro void class problem .com print

傳送門

n點m邊的有向圖,

如果 一個玩家能從起點出發,走奇數步後不能再行動,

  輸出Win

  輸出路徑

否則如果 一個玩家能走無限步

  輸出Draw

否則

  輸出Lose

跑一遍dfs並判環

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 
 8 const int maxn = 1e5 + 10
; 9 vector<int> G[maxn]; 10 11 int n, m; 12 bool draw; 13 bool out[maxn]; 14 bool dp[maxn][2]; 15 int vis[maxn][2], nxt[maxn][2]; 16 17 bool dfs(int x, int s) { 18 if (!out[x]) return s == 1; 19 if (vis[x][s] == 2) return dp[x][s]; 20 vis[x][s] = 1; 21 bool flag = 0; 22 for
(int i = 0; i < G[x].size(); i++) { 23 int to = G[x][i]; 24 if (vis[to][s ^ 1] == 1) {//註意不能為2 25 draw = 1; 26 continue; 27 } 28 if (dfs(to, s ^ 1)) { 29 flag = 1; 30 nxt[x][s] = to; 31 } 32 } 33 vis[x][s] = 2
; 34 return dp[x][s] = flag; 35 } 36 37 void print(int x, int s) { 38 printf("%d", x); 39 if (nxt[x][s]) { 40 printf(" "); 41 print(nxt[x][s], s ^ 1); 42 } else { 43 puts(""); 44 } 45 } 46 47 48 49 int main() { 50 scanf("%d%d", &n, &m); 51 for (int i = 1; i <= n; i++) { 52 int cnt; 53 scanf("%d", &cnt); 54 if (cnt) out[i] = 1; 55 while (cnt--) { 56 int to; 57 scanf("%d", &to); 58 G[i].push_back(to); 59 } 60 } 61 int st; 62 scanf("%d", &st); 63 if (dfs(st, 0)) { 64 puts("Win"); 65 print(st, 0); 66 } else if (draw) { 67 puts("Draw"); 68 } else { 69 puts("Lose"); 70 } 71 72 return 0; 73 }

Codeforces-936B Sleepy Game