Submission #367904

# Submission time Handle Problem Language Result Execution time Memory
367904 2021-02-18T19:09:28 Z NachoLibre Tropical Garden (IOI11_garden) C++17
100 / 100
2820 ms 36460 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(a) ((int)(a).size())
typedef vector<int> vint;
typedef vector<vint> vvint;
#ifndef wambule
#include "garden.h"
#include "gardenlib.h"
#else
void answer(int x) {
	cout << "[!] " << x << endl;
}
#endif

namespace blob {
	int p, pp;
}

void G(int x, int x2[], int x4[], int x5[], int x6[], int x7[], int x8[], int x9[], int &z) {
	x7[x] = z;
	++z;
	int y = x6[x];
	if(x7[y]) {
		if(x5[y]) {
			x2[x] = -1;
			if(x != blob::p) x4[x] = (x4[y] == -1 ? -1 : x4[y] + 1);
			if(x != blob::pp) x9[x] = (x9[y] == -1 ? -1 : x9[y] + 1);
			x5[x] = 1;
		} else {
			if(x7[blob::p] >= x7[y]) {
				if(x != blob::p) x4[x] = x7[blob::p] - x7[y] + 1;
			}
			if(x7[blob::pp] >= x7[y]) {
				if(x != blob::pp) x9[x] = x7[blob::pp] - x7[y] + 1;
			}
			x2[x] = x7[x] - x7[y] + 1;
			x8[y] = 1;
		}
		x5[x] = 1;
		return;
	}
	G(y, x2, x4, x5, x6, x7, x8, x9, z);
	x2[x] = (x8[y] ? -1 : x2[y]);
	if(x != blob::p) x4[x] = (x4[y] == -1 ? -1 : x4[y] + 1);
	if(x != blob::pp) x9[x] = (x9[y] == -1 ? -1 : x9[y] + 1);
	x5[x] = 1;
}

void count_routes(int n, int m, int p, int r[][2], int q, int gg[]) {
	blob::p = p;
	int pp = p + n;
	blob::pp = pp;
	int N = 2 * n;
	int x2[N], x4[N], x5[N];
	int x6[N], x7[N], x8[N], x9[N];
	vint v[n];
	for(int i = 0; i < N; ++i) {
		x5[i] = x7[i] = x8[i] = 0;
		x4[i] = (i == p ? 0 : -1);
		x9[i] = (i == pp ? 0 : -1);
		x2[i] = -1;
	}
	for(int i = 0; i < m; ++i) {
		if(sz(v[r[i][0]]) < 2) v[r[i][0]].push_back(r[i][1]);
		if(sz(v[r[i][1]]) < 2) v[r[i][1]].push_back(r[i][0]);
	}
	for(int i = 0; i < n; ++i) {
		x6[i] = v[i][0];
		x6[i + n] = v[i][(sz(v[i]) == 2)];
	}
	for(int i = 0; i < N; ++i) {
		if(x6[x6[i]] == i || x6[x6[i]] == (i >= n ? i - n : i + n)) {
			x6[i] += n;
		}
	}
	#ifdef wambule
	for(int i = 0; i < N; ++i) {
		// cout << (i >= n ? i - n : i) << ", " << (i >= n) << " -> " << x6[i] << endl;
		cout << i << " " << x6[i] << endl;
	}
	#endif
	int z = 1;
	for(int i = 0; i < N; ++i) {
		int x = i;
		if(!x5[x]) {
			G(x, x2, x4, x5, x6, x7, x8, x9, z);
		}
	}
	for(int qi = 0; qi < q; ++qi) {
		int g = gg[qi];
		int ap = 0;
		for(int i = 0; i < n; ++i) {
			if(x4[i] != -1) {
				if(x2[p] == -1) {
					if(x4[i] == g) {
						++ap;
						continue;
					}
				} else {
					if(g >= x4[i]) {
						if((g - x4[i]) % x2[p] == 0) {
							++ap;
							continue;
						}
					}
				}
			}
			if(x9[i] != -1) {
				if(x2[pp] == -1) {
					if(x9[i] == g) {
						++ap;
						continue;
					}
				} else {
					if(g >= x9[i]) {
						if((g - x9[i]) % x2[pp] == 0) {
							++ap;
							continue;
						}
					}
				}
			}
			// cout << "[ O ] " << i << endl;
		}
		answer(ap);
	}
	#ifdef wambule
	for(int i = 0; i < N; ++i) {
		cout << i << " - " << x2[i] << " " << x4[i] << " " << x9[i] << endl;
	}
	#endif
}

#ifdef wambule
#define C

#ifdef A
int n = 6;
int m = 6;
int p = 0;
int q = 2;
int g[] = {3, 102};
int r[][2] = {1, 2, 0, 1, 0, 3, 3, 4, 4, 5, 1, 5};
#endif
#ifdef B
int n = 5;
int m = 5;
int p = 2;
int q = 2;
int g[] = {3, 1};
int r[][2] = {1, 0, 1, 2, 3, 2, 1, 3, 4, 2};
#endif
#ifdef C
int n = 5;
int m = 5;
int p = 0;
int q = 2;
int g[] = {3, 5};
int r[][2] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0};
#endif

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	count_routes(n, m, p, r, q, g);
	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 10 ms 3692 KB Output is correct
12 Correct 22 ms 5996 KB Output is correct
13 Correct 47 ms 29932 KB Output is correct
14 Correct 76 ms 17992 KB Output is correct
15 Correct 76 ms 18156 KB Output is correct
16 Correct 61 ms 13420 KB Output is correct
17 Correct 54 ms 11464 KB Output is correct
18 Correct 23 ms 5484 KB Output is correct
19 Correct 74 ms 18028 KB Output is correct
20 Correct 111 ms 18284 KB Output is correct
21 Correct 59 ms 13164 KB Output is correct
22 Correct 55 ms 11372 KB Output is correct
23 Correct 80 ms 20204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 10 ms 3692 KB Output is correct
12 Correct 22 ms 5996 KB Output is correct
13 Correct 47 ms 29932 KB Output is correct
14 Correct 76 ms 17992 KB Output is correct
15 Correct 76 ms 18156 KB Output is correct
16 Correct 61 ms 13420 KB Output is correct
17 Correct 54 ms 11464 KB Output is correct
18 Correct 23 ms 5484 KB Output is correct
19 Correct 74 ms 18028 KB Output is correct
20 Correct 111 ms 18284 KB Output is correct
21 Correct 59 ms 13164 KB Output is correct
22 Correct 55 ms 11372 KB Output is correct
23 Correct 80 ms 20204 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 111 ms 3820 KB Output is correct
26 Correct 155 ms 6124 KB Output is correct
27 Correct 2510 ms 30088 KB Output is correct
28 Correct 1076 ms 18008 KB Output is correct
29 Correct 2798 ms 18292 KB Output is correct
30 Correct 1570 ms 13420 KB Output is correct
31 Correct 1525 ms 11500 KB Output is correct
32 Correct 173 ms 5824 KB Output is correct
33 Correct 1071 ms 18136 KB Output is correct
34 Correct 2820 ms 18296 KB Output is correct
35 Correct 1700 ms 13180 KB Output is correct
36 Correct 1537 ms 11556 KB Output is correct
37 Correct 830 ms 20364 KB Output is correct
38 Correct 2230 ms 36460 KB Output is correct