-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathD.cpp
130 lines (114 loc) · 1.8 KB
/
D.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define siz 100009
#define pb push_back
vector <ll> graph[siz];
vector <ll> result;
ll s, dp[100009][2], m, visit[100009][2];
ll solve(ll u, ll id, ll counter)
{
if(visit[u][id] && counter > m + 1) {
return 2;
}
if(counter >= 1000000) {
return 2;
}
visit[u][id] = 1;
if(dp[u][id] != -1)
return dp[u][id];
if(graph[u].size() == 0) {
if(id == 0)
return dp[u][id] = 1;
else {
result.pb(u);
return dp[u][id] = 0;
}
}
ll tmp = 0;
for(ll i = 0; i < graph[u].size(); i++) {
ll state = solve(graph[u][i], id ^ 1, counter + 1);
if(state == 0) {
result.pb(u);
return dp[u][id] = 0;
}
if(state == 2)
tmp = 1;
}
if(tmp == 1) {
return dp[u][id] = 2;
}
else
return dp[u][id] = 1;
}
int main()
{
ll n;
cin >> n >> m;
memset(dp, -1, sizeof(dp));
for(ll i = 1; i <= n; i++) {
ll cnt;
scanf("%lld", &cnt);
for(ll j = 1; j <= cnt; j++) {
ll v;
scanf("%lld", &v);
graph[i].pb(v);
}
}
scanf("%lld", &s);
ll state = solve(s, 0, 0);
if(state == 0) {
cout << "Win" << endl;
for(ll i = result.size() - 1; i >= 0; i--) {
printf("%lld", result[i]);
if(i != 0)
printf(" ");
else
printf("\n");
}
}
else if(state == 1)
cout << "Lose" << endl;
else
cout << "Draw" << endl;
return 0;
}
/*
15 20
3 4 9 7
0
1 1
3 5 6 1
1 13
0
4 8 15 4 2
1 7
1 2
0
1 4
0
2 3 11
1 5
2 1 6
4
*/
/*
5 5
1 2
1 3
1 4
2 2 5
0
1
*/
/*
6 6
1 2
2 3 4
1 5
1 5
1 6
0
1
Lose
*/