Submission #80057

# Submission time Handle Problem Language Result Execution time Memory
80057 2018-10-18T13:55:01 Z SpeedOfMagic Tropical Garden (IOI11_garden) C++14
100 / 100
2626 ms 12424 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>

using namespace std;
const int MX = 150000;
int g[2 * MX];
int cycle[2 * MX];
char vis[2 * MX];
int turns[2 * MX][2];

void dfs(int cur, int z) {
	if (cycle[cur] != -1) {
		turns[cur][z] = cycle[cur];
		return;
	}
	
	vis[cur] = 1;
	if (!vis[g[cur]])
		dfs(g[cur], z);
	turns[cur][z] = turns[g[cur]][z] + 1;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
	int fs[N], sc[N];
	for (int i = 0; i < N; i++)
		fs[i] = -1;
	for (int i = M - 1; i >= 0; i--) {
		sc[R[i][1]] = fs[R[i][1]];
		fs[R[i][1]] = R[i][0];
		sc[R[i][0]] = fs[R[i][0]];
		fs[R[i][0]] = R[i][1];
	}
	
	for (int i = 0; i < 2 * N; i++) 
		if (i >= N) {
			if (sc[i - N] == -1)
				g[i] = g[i - N];
			else if (fs[sc[i - N]] == i - N)
				g[i] = sc[i - N] + N;
			else
				g[i] = sc[i - N];
		} else {
			if (fs[fs[i]] == i)
				g[i] = fs[i] + N;
			else
				g[i] = fs[i];
		}
	
	int cycleSize[2];
	for (int z = 0; z < 2; z++) {
		for (int i = 0; i < 2 * N; i++)
			cycle[i] = -1;
		memset(vis, 0, sizeof vis);
		for (int i = 0; i < 2 * N; i++)
			turns[i][z] = 2000000000;
		
		cycleSize[z] = 0;
		int cur = P + N * z;
		while (!vis[cur]) {
			vis[cur] = 1;
			cycle[cur] = cycleSize[z]++;
			cur = g[cur];
		}
		
		for (int i = 0; i < 2 * N; i++)
			if (cycle[i] != -1)
				cycle[i] = (cycleSize[z] - cycle[i]) % cycleSize[z];
		
		if (cur != P + N * z) { //P is not in cycle
			for (int i = 0; i < 2 * N; i++)
				cycle[i] = -1;
			cycle[P + N * z] = 0;
			cycleSize[z] = 1;
		}
		
		memset(vis, 0, sizeof vis);
		for (int i = 0; i < 2 * N; i++)
			if (!vis[i])
				dfs(i, z);
	}
	
	//for (int i = 0; i < 2 * N; i++)
		//cout << i << ": " << turns[i][0] << " " << turns[i][1] << " " << g[i] << " " << fs[i] << " " << sc[i] << endl;
	
	for (int i = 0; i < Q; i++) {
		int ans = 0;
		for (int j = 0; j < N; j++)
			ans += ((turns[j][0] == G[i] ||
					(cycleSize[0] > 1 && G[i] >= turns[j][0] && (G[i] - turns[j][0]) % cycleSize[0] == 0)) ||
					(turns[j][1] == G[i] ||
					(cycleSize[1] > 1 && G[i] >= turns[j][1] && (G[i] - turns[j][1]) % cycleSize[1] == 0)));
		
		answer(ans);
	}
}


# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 7 ms 1884 KB Output is correct
12 Correct 20 ms 2632 KB Output is correct
13 Correct 28 ms 4656 KB Output is correct
14 Correct 56 ms 7032 KB Output is correct
15 Correct 58 ms 7132 KB Output is correct
16 Correct 49 ms 5368 KB Output is correct
17 Correct 47 ms 4740 KB Output is correct
18 Correct 22 ms 2484 KB Output is correct
19 Correct 57 ms 7120 KB Output is correct
20 Correct 60 ms 7132 KB Output is correct
21 Correct 51 ms 5368 KB Output is correct
22 Correct 50 ms 4780 KB Output is correct
23 Correct 56 ms 8000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 7 ms 1884 KB Output is correct
12 Correct 20 ms 2632 KB Output is correct
13 Correct 28 ms 4656 KB Output is correct
14 Correct 56 ms 7032 KB Output is correct
15 Correct 58 ms 7132 KB Output is correct
16 Correct 49 ms 5368 KB Output is correct
17 Correct 47 ms 4740 KB Output is correct
18 Correct 22 ms 2484 KB Output is correct
19 Correct 57 ms 7120 KB Output is correct
20 Correct 60 ms 7132 KB Output is correct
21 Correct 51 ms 5368 KB Output is correct
22 Correct 50 ms 4780 KB Output is correct
23 Correct 56 ms 8000 KB Output is correct
24 Correct 3 ms 632 KB Output is correct
25 Correct 111 ms 2036 KB Output is correct
26 Correct 147 ms 2936 KB Output is correct
27 Correct 1457 ms 4708 KB Output is correct
28 Correct 1369 ms 8776 KB Output is correct
29 Correct 1727 ms 8948 KB Output is correct
30 Correct 952 ms 7000 KB Output is correct
31 Correct 921 ms 6312 KB Output is correct
32 Correct 156 ms 3192 KB Output is correct
33 Correct 1353 ms 8776 KB Output is correct
34 Correct 1656 ms 8976 KB Output is correct
35 Correct 948 ms 6536 KB Output is correct
36 Correct 1411 ms 6008 KB Output is correct
37 Correct 955 ms 9464 KB Output is correct
38 Correct 2626 ms 12424 KB Output is correct