Submission #430450

# Submission time Handle Problem Language Result Execution time Memory
430450 2021-06-16T13:57:32 Z rainboy Toy Train (IOI17_train) C++
37 / 100
1903 ms 56712 KB
#include "train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

const int N = 5000, SMALL = 15, B = 14348907, M = 20000;

typedef vector<int> vi;

int *ej[N], eo[N], *fi[N], fo[N];

void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]); 
	ej[i][o] = j;
}

int cc[N], qu[N], cnt;

void dfs1(int i) {
	int o;

	if (cc[i])
		return;
	cc[i] = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs1(j);
	}
	qu[cnt++] = i;
}

vi rr; char has_[N]; int k, has;

void dfs2(int j, int c) {
	int o;

	if (cc[j] != -1) {
		if (cc[j] != c && has_[cc[j]])
			has_[c] = 1;
		return;
	}
	if (rr[j])
		has = 1;
	cc[j] = c, k++;
	for (o = fo[j]; o--; ) {
		int i = fi[j][o];

		dfs2(i, c);
	}
}

char self[N], right[N];
int adj[SMALL], mask[1 << SMALL], dp[B];

vi who_wins(vi aa, vi rr_, vi uu, vi vv) {
	int n = aa.size(), m = uu.size(), h, i, j, ka, kb;
	vi ans(n);

	rr = rr_;
	ka = kb = 0;
	for (i = 0; i < n; i++)
		if (aa[i] == 1)
			ka++;
		else
			kb++;
	if (n <= SMALL) {
		int x_, p, b;

		x_ = 0;
		for (i = 0; i < n; i++)
			if (!aa[i])
				x_ |= 1 << i;
		for (h = 0; h < m; h++)
			adj[vv[h]] |= 1 << uu[h];
		p = 1;
		for (i = 0; i < n; i++)
			mask[1 << i] = p, p *= 3;
		for (b = 1; b < 1 << n; b++)
			mask[b] = mask[b & b - 1] + mask[b & -b];
		for (b = p - 1; b >= 0; b--) {
			int b_, b1, b2, b1_, b2_;

			b1 = b2 = 0;
			for (i = 0, b_ = b; i < n; i++, b_ /= 3)
				if (b_ % 3 != 0) {
					b1 |= 1 << i;
					if (b_ % 3 == 2)
						b2 |= 1 << i;
				}
			dp[b] = x_;
			for (i = 0; i < n; i++)
				if ((b1 & 1 << i) == 0) {
					b1_ = b1 | 1 << i, b2_ = rr[i] ? b1_ : b2;
					if ((dp[mask[b1_] + mask[b2_]] & 1 << i) != 0)
						dp[b] |= adj[i] & b1 & ~x_;
					else
						dp[b] &= ~(adj[i] & b1 & x_);
				} else if ((b2 & 1 << i) != 0)
					dp[b] |= adj[i] & b1 & ~x_;
				else
					dp[b] &= ~(adj[i] & b1 & x_);
		}
		for (i = 0; i < n; i++)
			ans[i] = (dp[rr[i] ? mask[1 << i] + mask[1 << i] : mask[1 << i]] & 1 << i) != 0;
	} else if (ka == n) {
		for (i = 0; i < n; i++) {
			ej[i] = (int *) malloc(2 * sizeof *ej[i]);
			fi[i] = (int *) malloc(2 * sizeof *fi[i]);
		}
		for (h = 0; h < m; h++) {
			i = vv[h], j = uu[h];
			if (i == j)
				self[i] = 1;
			append(ej, eo, i, j), append(fi, fo, j, i);
		}
		for (i = 0; i < n; i++)
			if (!cc[i])
				dfs1(i);
		while (cnt--) {
			i = qu[cnt];
			if (cc[i] != -1)
				continue;
			k = 0, has = 0, dfs2(i, i);
			if ((k >= 2 || self[i]) && has)
				has_[i] = 1;
		}
		for (i = 0; i < n; i++)
			ans[i] = has_[cc[i]];
	} else if (kb == n) {
		for (i = 0; i < n; i++) {
			ej[i] = (int *) malloc(2 * sizeof *ej[i]);
			fi[i] = (int *) malloc(2 * sizeof *fi[i]);
		}
		for (h = 0; h < m; h++) {
			i = vv[h], j = uu[h];
			if (i == j)
				self[i] = 1;
			if (!rr[i] && !rr[j])
				append(ej, eo, i, j), append(fi, fo, j, i);
		}
		for (i = 0; i < n; i++)
			if (!cc[i])
				dfs1(i);
		while (cnt--) {
			i = qu[cnt];
			if (cc[i] != -1)
				continue;
			k = 0, has = 0, dfs2(i, i);
			if (k >= 2 || !rr[i] && self[i])
				has_[i] = 1;
		}
		for (h = 0; h < m; h++) {
			i = vv[h], j = uu[h];
			if (rr[i] || rr[j])
				append(ej, eo, i, j), append(fi, fo, j, i);
		}
		memset(cc, 0, n * sizeof *cc);
		for (i = 0; i < n; i++)
			if (has_[i])
				dfs1(i);
		for (i = 0; i < n; i++)
			ans[i] = cc[i] == 0;
	} else {
		memset(self, 0, n * sizeof *self), memset(right, 0, n * sizeof *right);
		for (h = 0; h < m; h++)
			if (vv[h] == uu[h])
				self[uu[h]] = 1;
			else
				right[uu[h]] = 1;
		for (i = 0; i < n; i++) {
			ans[i] = 0;
			for (j = i; j < n; j++)
				if (aa[j]) {
					if (rr[j]) {
						if (self[j]) {
							ans[i] = 1;
							break;
						}
					} else {
						if (!right[j]) {
							ans[i] = 0;
							break;
						}
					}
				} else {
					if (rr[j]) {
						if (!right[j]) {
							ans[i] = 1;
							break;
						}
					} else {
						if (self[j]) {
							ans[i] = 0;
							break;
						}
					}
				}
		}
	}
	return ans;
}

Compilation message

train.cpp: In function 'void append(int**, int*, int, int)':
train.cpp:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
train.cpp: In function 'vi who_wins(vi, vi, vi, vi)':
train.cpp:85:25: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   85 |    mask[b] = mask[b & b - 1] + mask[b & -b];
      |                       ~~^~~
train.cpp:155:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  155 |    if (k >= 2 || !rr[i] && self[i])
      |                  ~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 460 KB Output is correct
2 Correct 5 ms 460 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 4 ms 532 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 4 ms 536 KB Output is correct
7 Correct 4 ms 972 KB Output is correct
8 Correct 4 ms 972 KB Output is correct
9 Correct 4 ms 460 KB Output is correct
10 Correct 5 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1832 ms 56544 KB Output is correct
2 Correct 1748 ms 56524 KB Output is correct
3 Correct 1812 ms 56500 KB Output is correct
4 Correct 1768 ms 56520 KB Output is correct
5 Correct 1812 ms 56468 KB Output is correct
6 Correct 1858 ms 56628 KB Output is correct
7 Correct 1813 ms 56512 KB Output is correct
8 Correct 1737 ms 56612 KB Output is correct
9 Correct 1732 ms 56564 KB Output is correct
10 Correct 1761 ms 56580 KB Output is correct
11 Correct 1767 ms 56520 KB Output is correct
12 Correct 1723 ms 56712 KB Output is correct
13 Correct 1782 ms 56708 KB Output is correct
14 Correct 1829 ms 56648 KB Output is correct
15 Correct 1903 ms 56448 KB Output is correct
16 Correct 1706 ms 56580 KB Output is correct
17 Correct 1839 ms 56524 KB Output is correct
18 Correct 1787 ms 56444 KB Output is correct
19 Correct 1821 ms 56484 KB Output is correct
20 Correct 1893 ms 56488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1612 KB Output is correct
2 Correct 8 ms 1516 KB Output is correct
3 Correct 8 ms 1484 KB Output is correct
4 Correct 9 ms 1484 KB Output is correct
5 Correct 9 ms 1484 KB Output is correct
6 Correct 10 ms 1356 KB Output is correct
7 Correct 9 ms 1256 KB Output is correct
8 Correct 9 ms 1392 KB Output is correct
9 Correct 8 ms 1244 KB Output is correct
10 Correct 8 ms 1356 KB Output is correct
11 Correct 8 ms 1228 KB Output is correct
12 Correct 8 ms 1228 KB Output is correct
13 Correct 9 ms 1484 KB Output is correct
14 Correct 9 ms 1484 KB Output is correct
15 Correct 12 ms 1484 KB Output is correct
16 Correct 9 ms 1548 KB Output is correct
17 Correct 9 ms 1484 KB Output is correct
18 Correct 6 ms 1180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1100 KB Output is correct
2 Correct 11 ms 1248 KB Output is correct
3 Correct 12 ms 1292 KB Output is correct
4 Correct 9 ms 1356 KB Output is correct
5 Correct 9 ms 1356 KB Output is correct
6 Correct 8 ms 1356 KB Output is correct
7 Correct 9 ms 1356 KB Output is correct
8 Correct 12 ms 1228 KB Output is correct
9 Correct 7 ms 1228 KB Output is correct
10 Correct 8 ms 1356 KB Output is correct
11 Correct 8 ms 1256 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 9 ms 1388 KB Output is correct
14 Correct 8 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 616 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 6 ms 460 KB Output is correct
2 Correct 5 ms 460 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 4 ms 532 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 4 ms 536 KB Output is correct
7 Correct 4 ms 972 KB Output is correct
8 Correct 4 ms 972 KB Output is correct
9 Correct 4 ms 460 KB Output is correct
10 Correct 5 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 1832 ms 56544 KB Output is correct
13 Correct 1748 ms 56524 KB Output is correct
14 Correct 1812 ms 56500 KB Output is correct
15 Correct 1768 ms 56520 KB Output is correct
16 Correct 1812 ms 56468 KB Output is correct
17 Correct 1858 ms 56628 KB Output is correct
18 Correct 1813 ms 56512 KB Output is correct
19 Correct 1737 ms 56612 KB Output is correct
20 Correct 1732 ms 56564 KB Output is correct
21 Correct 1761 ms 56580 KB Output is correct
22 Correct 1767 ms 56520 KB Output is correct
23 Correct 1723 ms 56712 KB Output is correct
24 Correct 1782 ms 56708 KB Output is correct
25 Correct 1829 ms 56648 KB Output is correct
26 Correct 1903 ms 56448 KB Output is correct
27 Correct 1706 ms 56580 KB Output is correct
28 Correct 1839 ms 56524 KB Output is correct
29 Correct 1787 ms 56444 KB Output is correct
30 Correct 1821 ms 56484 KB Output is correct
31 Correct 1893 ms 56488 KB Output is correct
32 Correct 9 ms 1612 KB Output is correct
33 Correct 8 ms 1516 KB Output is correct
34 Correct 8 ms 1484 KB Output is correct
35 Correct 9 ms 1484 KB Output is correct
36 Correct 9 ms 1484 KB Output is correct
37 Correct 10 ms 1356 KB Output is correct
38 Correct 9 ms 1256 KB Output is correct
39 Correct 9 ms 1392 KB Output is correct
40 Correct 8 ms 1244 KB Output is correct
41 Correct 8 ms 1356 KB Output is correct
42 Correct 8 ms 1228 KB Output is correct
43 Correct 8 ms 1228 KB Output is correct
44 Correct 9 ms 1484 KB Output is correct
45 Correct 9 ms 1484 KB Output is correct
46 Correct 12 ms 1484 KB Output is correct
47 Correct 9 ms 1548 KB Output is correct
48 Correct 9 ms 1484 KB Output is correct
49 Correct 6 ms 1180 KB Output is correct
50 Correct 7 ms 1100 KB Output is correct
51 Correct 11 ms 1248 KB Output is correct
52 Correct 12 ms 1292 KB Output is correct
53 Correct 9 ms 1356 KB Output is correct
54 Correct 9 ms 1356 KB Output is correct
55 Correct 8 ms 1356 KB Output is correct
56 Correct 9 ms 1356 KB Output is correct
57 Correct 12 ms 1228 KB Output is correct
58 Correct 7 ms 1228 KB Output is correct
59 Correct 8 ms 1356 KB Output is correct
60 Correct 8 ms 1256 KB Output is correct
61 Correct 12 ms 1228 KB Output is correct
62 Correct 9 ms 1388 KB Output is correct
63 Correct 8 ms 1228 KB Output is correct
64 Incorrect 24 ms 616 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
65 Halted 0 ms 0 KB -