답안 #263338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
263338 2020-08-13T15:48:31 Z Berted 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
234 ms 58616 KB
#include "garden.h"
#include "gardenlib.h"
#include <vector>
#include <assert.h>
#include <iostream>
#include <algorithm>
#define vi vector<int>

const int INF = 1e9;

using namespace std;

vi sadj[150001], adj[300001];
int dst[2][300001], cyc[2];
vi ans[2][300001];

void DFS(int u, int id)
{
	// cout << u << " " << id << " " << dst[id][u] << "\n";
	for (auto v : adj[u])
	{
		if (dst[id][v] == INF)
		{
			dst[id][v] = dst[id][u] + 1;
			DFS(v, id);
		}
		else 
		{
			assert(cyc[id] == -1 && dst[id][v] == 0);
			cyc[id] = dst[id][u] + 1;
		}
	}
}

/*
	It is possible to represent the problem as a digraph with 2N nodes
	such that each node has at most one outgoing edge.
	We can use this property to determine which cycle lengths are valid.
*/

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
	for (int i = 0; i < M; i++)
	{
		sadj[R[i][0]].push_back(R[i][1]);
		sadj[R[i][1]].push_back(R[i][0]);
	}
	cyc[0] = cyc[1] = -1;
	for (int u = 0; u < N; u++)
	{
		for (int j = 0; j < min(2, (int)sadj[u].size()); j++)
		{
			int v = sadj[u][j];
			//cout << "Add edge " << 2 * u + j << " -> " << 2 * v + (sadj[v].size() > 1 && u == sadj[v][0]) << "\n";
			adj[2 * v + (sadj[v].size() > 1 && u == sadj[v][0])].push_back(2 * u + j);
		}
	}
	for (int i = 0; i < 2 * N; i++) {dst[0][i] = dst[1][i] = INF;}
	for (int i = 0; i < 2; i++)
	{
		dst[i][2 * P + i] = 0;
		DFS(2 * P + i, i);
		if (cyc[i] == -1)
		{
			for (int j = 0; j < 2 * N; j += 2)
			{
				if (dst[i][j] < INF) ans[i][0].push_back(dst[i][j]);
			}
			sort(ans[i][0].begin(), ans[i][0].end());
		}
		else
		{
			for (int j = 0; j < 2 * N; j += 2)
			{
				if (dst[i][j] < INF) ans[i][dst[i][j] % cyc[i]].push_back(dst[i][j]);
			}
			for (int j = 0; j < cyc[i]; j++)
			{
				sort(ans[i][j].begin(), ans[i][j].end());
			}
		}
	}
	for (int i = 0; i < Q; i++)
	{
		int ret = 0;
		for (int j = 0; j < 2; j++)
		{
			if (cyc[j] == -1)
			{
				ret += upper_bound(ans[j][0].begin(), ans[j][0].end(), G[i]) - lower_bound(ans[j][0].begin(), ans[j][0].end(), G[i]);
			}
			else
			{
				int v = G[i] % cyc[j];
				ret += upper_bound(ans[j][v].begin(), ans[j][v].end(), G[i]) - ans[j][v].begin();
			}
		}
		answer(ret);
	}
}


# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25216 KB Output is correct
2 Correct 18 ms 25088 KB Output is correct
3 Correct 18 ms 25216 KB Output is correct
4 Correct 17 ms 24960 KB Output is correct
5 Correct 18 ms 24960 KB Output is correct
6 Correct 22 ms 25344 KB Output is correct
7 Correct 22 ms 24960 KB Output is correct
8 Correct 19 ms 25168 KB Output is correct
9 Correct 20 ms 25344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25216 KB Output is correct
2 Correct 18 ms 25088 KB Output is correct
3 Correct 18 ms 25216 KB Output is correct
4 Correct 17 ms 24960 KB Output is correct
5 Correct 18 ms 24960 KB Output is correct
6 Correct 22 ms 25344 KB Output is correct
7 Correct 22 ms 24960 KB Output is correct
8 Correct 19 ms 25168 KB Output is correct
9 Correct 20 ms 25344 KB Output is correct
10 Correct 21 ms 24952 KB Output is correct
11 Correct 38 ms 27512 KB Output is correct
12 Correct 50 ms 29340 KB Output is correct
13 Correct 85 ms 51960 KB Output is correct
14 Correct 199 ms 39024 KB Output is correct
15 Correct 192 ms 39800 KB Output is correct
16 Correct 166 ms 36368 KB Output is correct
17 Correct 157 ms 34992 KB Output is correct
18 Correct 54 ms 29176 KB Output is correct
19 Correct 186 ms 38832 KB Output is correct
20 Correct 234 ms 39672 KB Output is correct
21 Correct 203 ms 36216 KB Output is correct
22 Correct 168 ms 34996 KB Output is correct
23 Correct 193 ms 40184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25216 KB Output is correct
2 Correct 18 ms 25088 KB Output is correct
3 Correct 18 ms 25216 KB Output is correct
4 Correct 17 ms 24960 KB Output is correct
5 Correct 18 ms 24960 KB Output is correct
6 Correct 22 ms 25344 KB Output is correct
7 Correct 22 ms 24960 KB Output is correct
8 Correct 19 ms 25168 KB Output is correct
9 Correct 20 ms 25344 KB Output is correct
10 Correct 21 ms 24952 KB Output is correct
11 Correct 38 ms 27512 KB Output is correct
12 Correct 50 ms 29340 KB Output is correct
13 Correct 85 ms 51960 KB Output is correct
14 Correct 199 ms 39024 KB Output is correct
15 Correct 192 ms 39800 KB Output is correct
16 Correct 166 ms 36368 KB Output is correct
17 Correct 157 ms 34992 KB Output is correct
18 Correct 54 ms 29176 KB Output is correct
19 Correct 186 ms 38832 KB Output is correct
20 Correct 234 ms 39672 KB Output is correct
21 Correct 203 ms 36216 KB Output is correct
22 Correct 168 ms 34996 KB Output is correct
23 Correct 193 ms 40184 KB Output is correct
24 Correct 20 ms 25088 KB Output is correct
25 Correct 32 ms 27512 KB Output is correct
26 Correct 49 ms 29208 KB Output is correct
27 Correct 97 ms 51960 KB Output is correct
28 Correct 167 ms 38904 KB Output is correct
29 Correct 208 ms 39800 KB Output is correct
30 Correct 172 ms 36472 KB Output is correct
31 Correct 144 ms 34936 KB Output is correct
32 Correct 51 ms 29304 KB Output is correct
33 Correct 169 ms 38904 KB Output is correct
34 Correct 198 ms 39800 KB Output is correct
35 Correct 182 ms 36472 KB Output is correct
36 Correct 139 ms 34996 KB Output is correct
37 Correct 173 ms 40216 KB Output is correct
38 Correct 157 ms 58616 KB Output is correct