#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010;
#include "train.h"
int dp[MAX_N], w[MAX_N], sum[MAX_N], inst[MAX_N], n;
int sure[MAX_N];
int belong[MAX_N];
vector<int> edge[MAX_N];
bool dfs(int x, int s, int v = 0) {
static vector<int> stk;
if (sure[x] != -1) return sure[x] == s;
if (dp[x] != -1) return dp[x] == s;
stk.pb(x);
inst[x] = true;
v += w[x];
sum[x] = v;
bool ret = true;
// A owns it
if (belong[x] == 1) {
dp[x] = 0;
for (int u : edge[x]) {
if (inst[u]) {
if (w[u] || sum[x] != sum[u]) {
dp[x] = 1;
break;
}
continue;
}
if (dfs(u, 1, v)) {
dp[x] = 1;
break;
}
}
}
else {
dp[x] = 1;
for (int u : edge[x]) {
if (inst[u]) {
if (w[u] == 0 && sum[x] == sum[u]) {
dp[x] = 0;
break;
}
continue;
}
if (dfs(u, 0, v)) {
dp[x] = 0;
break;
}
}
}
ret = dp[x] == s;
stk.pop_back();
inst[x] = false;
dp[x] = -1;
return ret;
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
n = a.size();
for (int i = 0;i < n;++i) {
w[i] = r[i];
belong[i] = a[i];
}
for (int i = 0;i < u.size();++i) {
edge[ u[i] ].pb( v[i] );
}
memset(sure, -1, sizeof(sure));
vector<int> res(n);
for (int i = 0;i < n;++i) {
memset(dp, -1, sizeof(dp));
res[i] = dfs(i, 1);
}
return res;
}
Compilation message
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0;i < u.size();++i) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
10100 KB |
Output is correct |
2 |
Correct |
213 ms |
10212 KB |
Output is correct |
3 |
Correct |
213 ms |
10112 KB |
Output is correct |
4 |
Correct |
211 ms |
10104 KB |
Output is correct |
5 |
Correct |
211 ms |
10224 KB |
Output is correct |
6 |
Correct |
215 ms |
10112 KB |
Output is correct |
7 |
Correct |
210 ms |
10104 KB |
Output is correct |
8 |
Correct |
212 ms |
10092 KB |
Output is correct |
9 |
Correct |
213 ms |
10452 KB |
Output is correct |
10 |
Correct |
210 ms |
10088 KB |
Output is correct |
11 |
Correct |
209 ms |
10060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
8 ms |
9668 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9720 KB |
Output is correct |
16 |
Correct |
8 ms |
9668 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
7 ms |
9596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2100 ms |
10416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2079 ms |
10460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2081 ms |
10564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
10100 KB |
Output is correct |
2 |
Correct |
213 ms |
10212 KB |
Output is correct |
3 |
Correct |
213 ms |
10112 KB |
Output is correct |
4 |
Correct |
211 ms |
10104 KB |
Output is correct |
5 |
Correct |
211 ms |
10224 KB |
Output is correct |
6 |
Correct |
215 ms |
10112 KB |
Output is correct |
7 |
Correct |
210 ms |
10104 KB |
Output is correct |
8 |
Correct |
212 ms |
10092 KB |
Output is correct |
9 |
Correct |
213 ms |
10452 KB |
Output is correct |
10 |
Correct |
210 ms |
10088 KB |
Output is correct |
11 |
Correct |
209 ms |
10060 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
7 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
7 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
8 ms |
9668 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
7 ms |
9720 KB |
Output is correct |
27 |
Correct |
8 ms |
9668 KB |
Output is correct |
28 |
Correct |
7 ms |
9676 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9596 KB |
Output is correct |
32 |
Execution timed out |
2100 ms |
10416 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |