Submission #39300

# Submission time Handle Problem Language Result Execution time Memory
39300 2018-01-11T03:50:02 Z 14kg Tropical Garden (IOI11_garden) C++11
100 / 100
3007 ms 8596 KB
#include "garden.h"
#include "gardenlib.h"
#include <algorithm>
#include <vector>
#define N 150001
#define MP 23

using namespace std;
int n, m, start, cycle1_len, cycle2_len;
int Sx, Sy, d[N][2], d1[N][2], d2[N][2];
pair<int, int> r[N];

void Push_r(int x, int y) {
	if (!r[x].first) r[x].first = y;
	else if (!r[x].second) r[x].second = y;
}
int dfs(int x, int y, int len) {
	d[x][y] = len;
	int nx = y ? r[x].second : r[x].first;
	int ny = r[nx].second && r[nx].first == x ? 1 : 0;

	if (d[nx][ny]) {
		if (d[nx][ny] == 1) return len;
		else return -1;
	}
	return dfs(nx, ny, len + 1);
}
int dfs_find1(int x, int y) {
	if (x == start && y == 0) return 0;
	if (d1[x][y] != 0) return d1[x][y];

	int nx = y ? r[x].second : r[x].first;
	int ny = r[nx].second && r[nx].first == x ? 1 : 0;

	d1[x][y] = -1, d1[x][y] = dfs_find1(nx, ny);
	if (d1[x][y] >= 0) d1[x][y]++;

	return d1[x][y];
}
int dfs_find2(int x, int y) {
	if (x == start && y == 1) return 0;
	if (d2[x][y] != 0) return d2[x][y];

	int nx = y ? r[x].second : r[x].first;
	int ny = r[nx].second && r[nx].first == x ? 1 : 0;

	d2[x][y] = -1, d2[x][y] = dfs_find2(nx, ny);
	if (d2[x][y] >= 0) d2[x][y]++;

	return d2[x][y];
}
void count_routes(int _n, int _m, int _s, int _r[][2], int Q_len, int Q[]) {
	n = _n, start = _s + 1;
	for (int i = 0; i < _m; i++) {
		int x = _r[i][0] + 1, y = _r[i][1] + 1;
		Push_r(x, y), Push_r(y, x);
	}

	cycle1_len = dfs(start, 0, 1);
	for (int i = 1; i <= n; i++) d[i][0] = d[i][1] = 0;
	cycle2_len = dfs(start, 1, 1);
	for (int i = 1; i <= n; i++) d[i][0] = d[i][1] = 0;
	
	for (int i = 1; i <= n; i++) {
		if (d1[i][0] == 0) dfs_find1(i, 0);
		if (d2[i][0] == 0) dfs_find2(i, 0);
		if (i == start) d1[i][0] = 0;
	}

	for (int i = 0; i < Q_len; i++) {
		int res = 0;
		if (Q[i] == 0) { answer(1); continue; }
		for (int j = 1; j <= n; j++) {
			if (d1[j][0] == Q[i] || d2[j][0] == Q[i]) res++;
			else if ((d1[j][0] > 0 || j == start) && cycle1_len > 0 && Q[i] >= d1[j][0] && (Q[i] - d1[j][0]) % cycle1_len == 0) res++;
			else if (d2[j][0] > 0 && cycle2_len > 0 && Q[i] >= d2[j][0] && (Q[i] - d2[j][0]) % cycle2_len == 0) res++;
		} answer(res);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 360 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 360 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 9 ms 1528 KB Output is correct
12 Correct 17 ms 2312 KB Output is correct
13 Correct 31 ms 6728 KB Output is correct
14 Correct 48 ms 6204 KB Output is correct
15 Correct 48 ms 5984 KB Output is correct
16 Correct 42 ms 4492 KB Output is correct
17 Correct 41 ms 3992 KB Output is correct
18 Correct 17 ms 1992 KB Output is correct
19 Correct 47 ms 5768 KB Output is correct
20 Correct 49 ms 5980 KB Output is correct
21 Correct 45 ms 4408 KB Output is correct
22 Correct 44 ms 3980 KB Output is correct
23 Correct 49 ms 6524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 360 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 9 ms 1528 KB Output is correct
12 Correct 17 ms 2312 KB Output is correct
13 Correct 31 ms 6728 KB Output is correct
14 Correct 48 ms 6204 KB Output is correct
15 Correct 48 ms 5984 KB Output is correct
16 Correct 42 ms 4492 KB Output is correct
17 Correct 41 ms 3992 KB Output is correct
18 Correct 17 ms 1992 KB Output is correct
19 Correct 47 ms 5768 KB Output is correct
20 Correct 49 ms 5980 KB Output is correct
21 Correct 45 ms 4408 KB Output is correct
22 Correct 44 ms 3980 KB Output is correct
23 Correct 49 ms 6524 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 128 ms 1356 KB Output is correct
26 Correct 218 ms 2176 KB Output is correct
27 Correct 1767 ms 6564 KB Output is correct
28 Correct 1455 ms 5864 KB Output is correct
29 Correct 1983 ms 5968 KB Output is correct
30 Correct 1051 ms 4504 KB Output is correct
31 Correct 1206 ms 3868 KB Output is correct
32 Correct 198 ms 2012 KB Output is correct
33 Correct 1465 ms 7412 KB Output is correct
34 Correct 1790 ms 7580 KB Output is correct
35 Correct 950 ms 5900 KB Output is correct
36 Correct 1813 ms 5424 KB Output is correct
37 Correct 1065 ms 8104 KB Output is correct
38 Correct 3007 ms 8596 KB Output is correct