Submission #551262

# Submission time Handle Problem Language Result Execution time Memory
551262 2022-04-20T07:34:49 Z Sohsoh84 Tropical Garden (IOI11_garden) C++17
100 / 100
2416 ms 48872 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define debug(x)			cerr << #x << ": " << x << endl;

const ll MAXN = 1e6 + 10;

int n, m, p, deg[MAXN], F[MAXN], dist[2][MAXN], cyc[2];
vector<int> adj[MAXN];
bool vis[MAXN];

inline void add_edge(int u, int v) {
	F[u] = v;
	adj[v].push_back(u);
}

inline void add(int u, int v) {
	if (deg[u] > 1) return;
	if (deg[u] == 0) {
		if (deg[v] == 0) add_edge(u << 1 | 1, v << 1);
		else add_edge(u << 1 | 1, v << 1 | 1);
	} else {
		if (deg[v] == 0) add_edge(u << 1, v << 1);
		else add_edge(u << 1, v << 1 | 1);
	}
}

void dfs(int v, int len, int ind) {
	vis[v] = true;
	dist[ind][v] = len;
	for (int u : adj[v]) {
		if (vis[u]) cyc[ind] = len;
		else
			dfs(u, len + 1, ind);
	}
}

inline bool f(int ind, int v, int k) {
	if (!dist[ind][v]) return false;
	k -= dist[ind][v] - 1;

	if (k == 0) return true;
	if (k < 0) return false;
	return cyc[ind] && k % cyc[ind] == 0;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
	n = N, m = M, p = P;
	p++;
	for (int i = 0; i < m; i++) {
		int u = R[i][0], v = R[i][1];
		u++;
		v++;

		add(u, v);
		add(v, u);
		deg[u]++;
		deg[v]++;
	}

	for (int v = 1; v <= n; v++)
		if (!F[v << 1])
			add_edge(v << 1, F[v << 1 | 1]);
	
	dfs(p << 1, 1, 0);
	memset(vis, false, sizeof vis);
	dfs(p << 1 | 1, 1, 1);

	for (int i = 0; i < Q; i++) {
		int ans = 0;
		for (int v = 1; v <= n; v++)
			ans += (f(0, v << 1 | 1, G[i]) || f(1, v << 1 | 1, G[i]));

		answer(ans);
	}
}


# Verdict Execution time Memory Grader output
1 Correct 12 ms 24916 KB Output is correct
2 Correct 14 ms 24796 KB Output is correct
3 Correct 12 ms 24916 KB Output is correct
4 Correct 12 ms 24788 KB Output is correct
5 Correct 12 ms 24692 KB Output is correct
6 Correct 12 ms 24916 KB Output is correct
7 Correct 13 ms 24788 KB Output is correct
8 Correct 14 ms 24888 KB Output is correct
9 Correct 14 ms 24896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24916 KB Output is correct
2 Correct 14 ms 24796 KB Output is correct
3 Correct 12 ms 24916 KB Output is correct
4 Correct 12 ms 24788 KB Output is correct
5 Correct 12 ms 24692 KB Output is correct
6 Correct 12 ms 24916 KB Output is correct
7 Correct 13 ms 24788 KB Output is correct
8 Correct 14 ms 24888 KB Output is correct
9 Correct 14 ms 24896 KB Output is correct
10 Correct 12 ms 24740 KB Output is correct
11 Correct 20 ms 26396 KB Output is correct
12 Correct 27 ms 27272 KB Output is correct
13 Correct 47 ms 43728 KB Output is correct
14 Correct 68 ms 33484 KB Output is correct
15 Correct 96 ms 34480 KB Output is correct
16 Correct 66 ms 31260 KB Output is correct
17 Correct 62 ms 30788 KB Output is correct
18 Correct 28 ms 27296 KB Output is correct
19 Correct 68 ms 34140 KB Output is correct
20 Correct 88 ms 34548 KB Output is correct
21 Correct 67 ms 31084 KB Output is correct
22 Correct 61 ms 30880 KB Output is correct
23 Correct 72 ms 34160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24916 KB Output is correct
2 Correct 14 ms 24796 KB Output is correct
3 Correct 12 ms 24916 KB Output is correct
4 Correct 12 ms 24788 KB Output is correct
5 Correct 12 ms 24692 KB Output is correct
6 Correct 12 ms 24916 KB Output is correct
7 Correct 13 ms 24788 KB Output is correct
8 Correct 14 ms 24888 KB Output is correct
9 Correct 14 ms 24896 KB Output is correct
10 Correct 12 ms 24740 KB Output is correct
11 Correct 20 ms 26396 KB Output is correct
12 Correct 27 ms 27272 KB Output is correct
13 Correct 47 ms 43728 KB Output is correct
14 Correct 68 ms 33484 KB Output is correct
15 Correct 96 ms 34480 KB Output is correct
16 Correct 66 ms 31260 KB Output is correct
17 Correct 62 ms 30788 KB Output is correct
18 Correct 28 ms 27296 KB Output is correct
19 Correct 68 ms 34140 KB Output is correct
20 Correct 88 ms 34548 KB Output is correct
21 Correct 67 ms 31084 KB Output is correct
22 Correct 61 ms 30880 KB Output is correct
23 Correct 72 ms 34160 KB Output is correct
24 Correct 13 ms 24764 KB Output is correct
25 Correct 86 ms 26512 KB Output is correct
26 Correct 100 ms 27356 KB Output is correct
27 Correct 2269 ms 43796 KB Output is correct
28 Correct 755 ms 34380 KB Output is correct
29 Correct 2410 ms 34576 KB Output is correct
30 Correct 1401 ms 31464 KB Output is correct
31 Correct 1431 ms 30868 KB Output is correct
32 Correct 112 ms 27340 KB Output is correct
33 Correct 725 ms 34052 KB Output is correct
34 Correct 2416 ms 34552 KB Output is correct
35 Correct 1557 ms 31372 KB Output is correct
36 Correct 1478 ms 30900 KB Output is correct
37 Correct 562 ms 34304 KB Output is correct
38 Correct 1993 ms 48872 KB Output is correct