Submission #775981

# Submission time Handle Problem Language Result Execution time Memory
775981 2023-07-07T08:02:45 Z Jomnoi Toy Train (IOI17_train) C++17
37 / 100
1149 ms 1600 KB
#include <bits/stdc++.h>
#include "train.h"
using namespace std;

const int MAX_N = 5005;
const int MAX_M = 20005;

int N, M;
bool ok;
vector <int> graph[MAX_N], rgraph[MAX_N];
int A[MAX_N], R[MAX_N], good[MAX_N];
bool visited[MAX_N];
bool self[MAX_N], nxt[MAX_N];
int st[MAX_N];
vector <int> ans;
vector <int> nodes;

bool solve(int u) {
	nodes.push_back(u);
	st[u] = nodes.size();
	bool win = false;
	for (auto v : graph[u]) {
		if (st[v] == 0) win |= (solve(v) ^ (A[u] != A[v]));
		else {
			bool good_cycle = false;
			for (int i = st[v] - 1; i < nodes.size(); i++) {
				if (R[nodes[i]] == 1) good_cycle = true;
			}

			if (good_cycle == true and A[u] == 1) win = true;
			else if (good_cycle == false and A[u] == 0) win = true;
		}
	}
	st[u] = 0;
	nodes.pop_back();
	return win;
}

bool dfs(int u, int x) {
	visited[u] = true;
	for (auto v : graph[u]) {
		if (v == x) return true;
		if (visited[v] == false and dfs(v, x) == true) return true;
	}
	return false;
}

bool dfs2(int u) {
	visited[u] = true;
	if (R[u] == 1) return true;
	for (auto v : graph[u]) {
		if (visited[v] == false and dfs2(v) == true) return true;
	}
	return false;
}

bool dfs3(int u, int x) {
	visited[u] = true;
	for (auto v : graph[u]) {
		if (v == x) return true;
		if (visited[v] == false and R[v] == 0 and dfs3(v, x) == true) return true;
	}
	return false;
}

void dfs4(int v) {
	visited[v] = true;
	ans[v] = 0;
	for (auto u : rgraph[v]) {
		if (visited[u] == false) dfs4(u);
	}
}

vector <int> who_wins(vector <int> a, vector <int> r, vector <int> u, vector <int> v) {
	N = a.size(), M = u.size();
	bool all_1 = true, sub1 = true;
	for (int i = 0; i < N; i++) {
		A[i] = a[i], R[i] = r[i];

		if (A[i] == 0) all_1 = false;
	}
	for (int i = 0; i < M; i++) {
		if (u[i] != v[i] and u[i] != v[i] - 1) sub1 = false;
		if (u[i] == v[i]) self[u[i]] = true;
		else if (u[i] + 1 == v[i]) nxt[u[i]] = true;

		graph[u[i]].push_back(v[i]);
		rgraph[v[i]].push_back(u[i]);
	}

	ans = vector <int> (N, 0);
	if (sub1 == true) {
		ok = R[N - 1];
		for (int i = N - 1; i >= 0; i--) {
			if (self[i] == true) {
				if (R[i] == 1 and A[i] == 1) ok = true;
				else if (R[i] == 0 and A[i] == 0) ok = false;
			}
			
			ans[i] = ok;

			if (i > 0 and nxt[i - 1] == false) ok = R[i - 1];
		}
	}
	else if (N <= 15) {
		for (int s = 0; s < N; s++) ans[s] = solve(s) ^ (A[s] == 0);
	}
	else if (all_1 == true) {
		for (int s = 0; s < N; s++) {
			if (R[s] == 0) continue;
			for (int i = 0; i < N; i++) visited[i] = false;

			if (dfs(s, s) == false) R[s] = 0;
		}
		for (int s = 0; s < N; s++) {
			for (int i = 0; i < N; i++) visited[i] = false;

			ans[s] = dfs2(s);
		}
	}
	else {
		ans = vector <int> (N, 1);
		for (int s = 0; s < N; s++) {
			if (R[s] == 1) continue;
			for (int i = 0; i < N; i++) visited[i] = false;

			if (dfs3(s, s) == false) continue;

			for (int i = 0; i < N; i++) visited[i] = false;
			dfs4(s);
		}
	}
	return ans;
}

Compilation message

train.cpp: In function 'bool solve(int)':
train.cpp:26:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |    for (int i = st[v] - 1; i < nodes.size(); i++) {
      |                            ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1108 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
3 Correct 3 ms 1108 KB Output is correct
4 Correct 3 ms 1012 KB Output is correct
5 Correct 3 ms 1108 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1108 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1108 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 556 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 234 ms 544 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 4 ms 560 KB Output is correct
9 Correct 80 ms 540 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 23 ms 552 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 1600 KB Output is correct
2 Correct 128 ms 1572 KB Output is correct
3 Correct 167 ms 1576 KB Output is correct
4 Correct 92 ms 1532 KB Output is correct
5 Correct 110 ms 1500 KB Output is correct
6 Correct 425 ms 1428 KB Output is correct
7 Correct 144 ms 1400 KB Output is correct
8 Correct 87 ms 1364 KB Output is correct
9 Correct 177 ms 1404 KB Output is correct
10 Correct 74 ms 1396 KB Output is correct
11 Correct 114 ms 1372 KB Output is correct
12 Correct 20 ms 1336 KB Output is correct
13 Correct 407 ms 1548 KB Output is correct
14 Correct 766 ms 1548 KB Output is correct
15 Correct 526 ms 1568 KB Output is correct
16 Correct 16 ms 1364 KB Output is correct
17 Correct 60 ms 1468 KB Output is correct
18 Correct 272 ms 1324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1208 KB Output is correct
2 Correct 76 ms 1332 KB Output is correct
3 Correct 125 ms 1408 KB Output is correct
4 Correct 77 ms 1456 KB Output is correct
5 Correct 254 ms 1364 KB Output is correct
6 Correct 196 ms 1424 KB Output is correct
7 Correct 191 ms 1364 KB Output is correct
8 Correct 83 ms 1380 KB Output is correct
9 Correct 5 ms 1368 KB Output is correct
10 Correct 6 ms 1364 KB Output is correct
11 Correct 6 ms 1364 KB Output is correct
12 Correct 6 ms 1380 KB Output is correct
13 Correct 433 ms 1480 KB Output is correct
14 Correct 215 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1149 ms 1528 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1108 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
3 Correct 3 ms 1108 KB Output is correct
4 Correct 3 ms 1012 KB Output is correct
5 Correct 3 ms 1108 KB Output is correct
6 Correct 3 ms 1108 KB Output is correct
7 Correct 3 ms 1108 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1108 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 2 ms 556 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 234 ms 544 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 4 ms 560 KB Output is correct
20 Correct 80 ms 540 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 23 ms 552 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 0 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 122 ms 1600 KB Output is correct
33 Correct 128 ms 1572 KB Output is correct
34 Correct 167 ms 1576 KB Output is correct
35 Correct 92 ms 1532 KB Output is correct
36 Correct 110 ms 1500 KB Output is correct
37 Correct 425 ms 1428 KB Output is correct
38 Correct 144 ms 1400 KB Output is correct
39 Correct 87 ms 1364 KB Output is correct
40 Correct 177 ms 1404 KB Output is correct
41 Correct 74 ms 1396 KB Output is correct
42 Correct 114 ms 1372 KB Output is correct
43 Correct 20 ms 1336 KB Output is correct
44 Correct 407 ms 1548 KB Output is correct
45 Correct 766 ms 1548 KB Output is correct
46 Correct 526 ms 1568 KB Output is correct
47 Correct 16 ms 1364 KB Output is correct
48 Correct 60 ms 1468 KB Output is correct
49 Correct 272 ms 1324 KB Output is correct
50 Correct 5 ms 1208 KB Output is correct
51 Correct 76 ms 1332 KB Output is correct
52 Correct 125 ms 1408 KB Output is correct
53 Correct 77 ms 1456 KB Output is correct
54 Correct 254 ms 1364 KB Output is correct
55 Correct 196 ms 1424 KB Output is correct
56 Correct 191 ms 1364 KB Output is correct
57 Correct 83 ms 1380 KB Output is correct
58 Correct 5 ms 1368 KB Output is correct
59 Correct 6 ms 1364 KB Output is correct
60 Correct 6 ms 1364 KB Output is correct
61 Correct 6 ms 1380 KB Output is correct
62 Correct 433 ms 1480 KB Output is correct
63 Correct 215 ms 1364 KB Output is correct
64 Incorrect 1149 ms 1528 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
65 Halted 0 ms 0 KB -