Submission #430271

# Submission time Handle Problem Language Result Execution time Memory
430271 2021-06-16T12:37:10 Z rainboy Toy Train (IOI17_train) C++
27 / 100
31 ms 1612 KB
#include "train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

const int N = 5000, SMALL = 15, 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];

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 (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:115:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  115 |    if (k >= 2 || !rr[i] && self[i])
      |                  ~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 460 KB Output is correct
2 Correct 6 ms 460 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 5 ms 460 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 5 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 4 ms 460 KB Output is correct
11 Correct 5 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1612 KB Output is correct
2 Correct 8 ms 1508 KB Output is correct
3 Correct 9 ms 1508 KB Output is correct
4 Correct 14 ms 1484 KB Output is correct
5 Correct 9 ms 1356 KB Output is correct
6 Correct 9 ms 1356 KB Output is correct
7 Correct 9 ms 1352 KB Output is correct
8 Correct 8 ms 1356 KB Output is correct
9 Correct 8 ms 1228 KB Output is correct
10 Correct 8 ms 1240 KB Output is correct
11 Correct 8 ms 1240 KB Output is correct
12 Correct 10 ms 1228 KB Output is correct
13 Correct 8 ms 1484 KB Output is correct
14 Correct 9 ms 1484 KB Output is correct
15 Correct 9 ms 1484 KB Output is correct
16 Correct 9 ms 1484 KB Output is correct
17 Correct 9 ms 1484 KB Output is correct
18 Correct 8 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1100 KB Output is correct
2 Correct 8 ms 1440 KB Output is correct
3 Correct 10 ms 1484 KB Output is correct
4 Correct 9 ms 1484 KB Output is correct
5 Correct 9 ms 1596 KB Output is correct
6 Correct 9 ms 1576 KB Output is correct
7 Correct 12 ms 1488 KB Output is correct
8 Correct 9 ms 1496 KB Output is correct
9 Correct 11 ms 1356 KB Output is correct
10 Correct 10 ms 1456 KB Output is correct
11 Correct 12 ms 1560 KB Output is correct
12 Correct 10 ms 1484 KB Output is correct
13 Correct 9 ms 1588 KB Output is correct
14 Correct 10 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 31 ms 716 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 460 KB Output is correct
2 Correct 6 ms 460 KB Output is correct
3 Correct 6 ms 460 KB Output is correct
4 Correct 5 ms 460 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 5 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 4 ms 460 KB Output is correct
11 Correct 5 ms 460 KB Output is correct
12 Incorrect 1 ms 204 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
13 Halted 0 ms 0 KB -