Submission #551309

# Submission time Handle Problem Language Result Execution time Memory
551309 2022-04-20T08:59:39 Z alireza_kaviani Tropical Garden (IOI11_garden) C++17
100 / 100
2869 ms 61204 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;

#define sep   " "
#define SZ(x) int((x).size())

const int MAXN = 3e5 + 10;

int n , m , p , nxt[MAXN] , V[MAXN] , U[MAXN] , comp[MAXN] , mark[MAXN] , H[MAXN] , ind[MAXN] , valid[MAXN] , first[MAXN];
vector<int> E[MAXN] , vec , cycle[MAXN] , adj[MAXN] , par[MAXN];

void PreDFS(int v){
	mark[v] = 1;
	if(!mark[nxt[v]]){
		PreDFS(nxt[v]);
	}
	vec.push_back(v);
}

void DFS(int v , int cmp , int root , int h = 0){
	if(valid[v])	vec.push_back(h);
	par[v] = vec;
	H[v] = h; comp[v] = cmp; ind[v] = root;
	for(int u : adj[v]){
		if(comp[u] != -1)	continue;
		DFS(u , cmp , root , h + 1);
	}
	if(valid[v])	vec.pop_back();
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
	fill(comp , comp + MAXN , -1);
	fill(first , first + MAXN , -1);
	n = N; m = M; p = P;
	for(int i = 0 ; i < m ; i++){
		V[i] = R[i][0]; U[i] = R[i][1];
		E[V[i]].push_back(2 * i);
		E[U[i]].push_back(2 * i + 1);
		if(first[V[i]] == -1){
			first[V[i]] = 2 * i + 1;
		}
		if(first[U[i]] == -1){
			first[U[i]] = 2 * i;
		}
	}
	for(int i = 0 ; i < n ; i++){
		for(int j = 1 ; j < SZ(E[i]) ; j++){
			nxt[E[i][j]] = (E[i][0] ^ 1);
		}
		nxt[E[i][0]] = (E[i][1 % SZ(E[i])] ^ 1);
	}
	for(int i : E[p]){
		valid[i] = 1;
	}
	for(int i = 0 ; i < 2 * m ; i++){
		adj[nxt[i]].push_back(i);
//		cout << i << sep << nxt[i] << endl;
	}
	for(int i = 0 ; i < 2 * m ; i++){
		if(comp[i] != -1)	continue;
		vec.clear();
		PreDFS(i);
		int v = vec[0];
		while(comp[v] == -1){
			comp[v] = i;
			cycle[i].push_back(v);
			v = nxt[v];
		}
		for(int j = 0 ; j < SZ(cycle[i]) ; j++){
			vec.clear();
			DFS(cycle[i][j] , i , j , 0);
		}
	}
	for(int i = 0 ; i < Q ; i++){
		int k = G[i] - 1 , ans = 0;
		for(int j = 0 ; j < n ; j++){
			int e = first[j];
			if(H[e] <= k){
				int x = cycle[comp[e]][(k - H[e] + ind[e]) % SZ(cycle[comp[e]])];
				ans += valid[x];
//				cout << j << sep << e << sep << x << sep << valid[x] << endl;
			}
			else{
//				cout << "Debug " << j << endl;
				for(int x : par[e]){
					if(H[e] - x == k){
						ans++;
					}
				}
			}
		}
//		cout << ans << endl;
		answer(ans);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31188 KB Output is correct
2 Correct 16 ms 31060 KB Output is correct
3 Correct 17 ms 31060 KB Output is correct
4 Correct 17 ms 30792 KB Output is correct
5 Correct 17 ms 30796 KB Output is correct
6 Correct 19 ms 31220 KB Output is correct
7 Correct 18 ms 30932 KB Output is correct
8 Correct 17 ms 30984 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31188 KB Output is correct
2 Correct 16 ms 31060 KB Output is correct
3 Correct 17 ms 31060 KB Output is correct
4 Correct 17 ms 30792 KB Output is correct
5 Correct 17 ms 30796 KB Output is correct
6 Correct 19 ms 31220 KB Output is correct
7 Correct 18 ms 30932 KB Output is correct
8 Correct 17 ms 30984 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 15 ms 30848 KB Output is correct
11 Correct 28 ms 34320 KB Output is correct
12 Correct 54 ms 37184 KB Output is correct
13 Correct 65 ms 50628 KB Output is correct
14 Correct 164 ms 49160 KB Output is correct
15 Correct 153 ms 50668 KB Output is correct
16 Correct 152 ms 46500 KB Output is correct
17 Correct 167 ms 47936 KB Output is correct
18 Correct 54 ms 37240 KB Output is correct
19 Correct 149 ms 49396 KB Output is correct
20 Correct 163 ms 50484 KB Output is correct
21 Correct 162 ms 46416 KB Output is correct
22 Correct 156 ms 51120 KB Output is correct
23 Correct 151 ms 50948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31188 KB Output is correct
2 Correct 16 ms 31060 KB Output is correct
3 Correct 17 ms 31060 KB Output is correct
4 Correct 17 ms 30792 KB Output is correct
5 Correct 17 ms 30796 KB Output is correct
6 Correct 19 ms 31220 KB Output is correct
7 Correct 18 ms 30932 KB Output is correct
8 Correct 17 ms 30984 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 15 ms 30848 KB Output is correct
11 Correct 28 ms 34320 KB Output is correct
12 Correct 54 ms 37184 KB Output is correct
13 Correct 65 ms 50628 KB Output is correct
14 Correct 164 ms 49160 KB Output is correct
15 Correct 153 ms 50668 KB Output is correct
16 Correct 152 ms 46500 KB Output is correct
17 Correct 167 ms 47936 KB Output is correct
18 Correct 54 ms 37240 KB Output is correct
19 Correct 149 ms 49396 KB Output is correct
20 Correct 163 ms 50484 KB Output is correct
21 Correct 162 ms 46416 KB Output is correct
22 Correct 156 ms 51120 KB Output is correct
23 Correct 151 ms 50948 KB Output is correct
24 Correct 18 ms 30964 KB Output is correct
25 Correct 300 ms 34256 KB Output is correct
26 Correct 539 ms 37280 KB Output is correct
27 Correct 1201 ms 50756 KB Output is correct
28 Correct 2561 ms 49484 KB Output is correct
29 Correct 2613 ms 51240 KB Output is correct
30 Correct 1581 ms 46548 KB Output is correct
31 Correct 1560 ms 47116 KB Output is correct
32 Correct 595 ms 37384 KB Output is correct
33 Correct 2550 ms 49676 KB Output is correct
34 Correct 2596 ms 50216 KB Output is correct
35 Correct 1704 ms 46540 KB Output is correct
36 Correct 1511 ms 49080 KB Output is correct
37 Correct 2569 ms 50988 KB Output is correct
38 Correct 2869 ms 61204 KB Output is correct