Submission #751186

# Submission time Handle Problem Language Result Execution time Memory
751186 2023-05-31T07:39:10 Z minhcool Tropical Garden (IOI11_garden) C++17
69 / 100
5000 ms 64912 KB
#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

//#define ll long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair

/*
Algo: We have 2n nodes (i, 0/1 - is last node the largest)
Just for lmao
*/

typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef pair<ii, ii> iiii;

const ll N = 3e5 + 5;

const ll oo = 1e18 + 7, mod = 1e9 + 7;

mt19937 rng(1);

ll rnd(ll l, ll r){
	ll temp = rng() % (r - l + 1);
	return abs(temp) + l;
}

ll n, m, p, q;
vector<ll> Adj[N]; 

// cycles that node u can get
vector<ii> cyc[N][2];

//map<ii, ll> mp;

ii nxt[N][2];

ll mn_dist[N][2];

vector<ii> Adj2[N][2];

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
	n = N, m = M, p = P;
	for(ll i = 0; i < m; i++){
		Adj[R[i][0]].pb(R[i][1]);
		Adj[R[i][1]].pb(R[i][0]);
		//mp[{min(R[i][0], R[i][1]), m}
	}
	for(ll i = 0; i < n; i++){
		// last trail is the best in all trails from i -> need to choose second if there is one
		if(Adj[i].size() >= 2) nxt[i][1] = {Adj[i][1], (Adj[Adj[i][1]][0] == i)};
		else nxt[i][1] = {Adj[i][0], (Adj[Adj[i][0]][0] == i)};
		nxt[i][0] = {Adj[i][0], (Adj[Adj[i][0]][0] == i)};
	}
	for(ll i = 0; i < n; i++){
		for(ll j = 0; j < 2; j++){
			Adj2[nxt[i][j].fi][nxt[i][j].se].pb({i, j});
		}
	}
	// finding the cycle for (p, 0)
	ii itr = {p, 0};
	ll len = 0;
	while((!len || itr != make_pair(p, 0LL)) && len <= 2 * n){
		len++;
		itr = nxt[itr.fi][itr.se];
	}
	if(len > 2 * n) len = oo;
	for(ll i = 0; i < n; i++) mn_dist[i][0] = mn_dist[i][1] = oo;
	mn_dist[p][0] = 0;
	queue<ii> bfs;
	bfs.push({p, 0});
	while(!bfs.empty()){
		ii u = bfs.front();
		bfs.pop();
		for(auto it : Adj2[u.fi][u.se]){
			if(mn_dist[it.fi][it.se] != oo) continue;
			mn_dist[it.fi][it.se] = mn_dist[u.fi][u.se] + 1;
			bfs.push(it);
		}
	}
	for(ll i = 0; i < n; i++){
		for(ll j = 0; j < 2; j++){
			if(mn_dist[i][j] == oo) continue;
			cyc[i][j].pb({mn_dist[i][j], len});	
		}
	}
	itr = {p, 1};
	len = 0;
	while((!len || itr != make_pair(p, 1LL)) && len <= 2 * n){
		len++;
		itr = nxt[itr.fi][itr.se];
	}
	if(len > 2 * n) len = oo;
	for(ll i = 0; i < n; i++) mn_dist[i][0] = mn_dist[i][1] = oo;
	mn_dist[p][1] = 0;
	//queue<ii> bfs;
	bfs.push({p, 1});
	//cout << "OKAY\n";
	while(!bfs.empty()){
		ii u = bfs.front();
		bfs.pop();
		for(auto it : Adj2[u.fi][u.se]){
			if(mn_dist[it.fi][it.se] != oo) continue;
			mn_dist[it.fi][it.se] = mn_dist[u.fi][u.se] + 1;
			bfs.push(it);
		}
	}
	for(ll i = 0; i < n; i++){
		for(ll j = 0; j < 2; j++){
			if(mn_dist[i][j] == oo) continue;
			cyc[i][j].pb({mn_dist[i][j], len});	
			//cout << i << " " << j << " " << mn_dist[i][j] << " " << len << "\n";
		}
	}
//	exit(0);
	for(ll i = 0; i < Q; i++){
		ll tol = 0;
		G[i]--;
		for(ll j = 0; j < n; j++){
			ii temp = {Adj[j][0], (Adj[Adj[j][0]][0] == j)};
			//cout << temp.fi << " " << temp.se << "\n";
			//cout << j << " " << Adj[j][0] << "\n";
			//continue;
			bool ck = 0;
			for(auto it : cyc[temp.fi][temp.se]) ck |= ((G[i] >= it.fi) && !((G[i] - it.fi) % it.se));
			tol += ck;
		}
		//cout << i << " " << tol << "\n";
		answer(tol);
	//if(n >= 100000 && Q > 1) exit(0);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 35668 KB Output is correct
2 Correct 17 ms 35680 KB Output is correct
3 Correct 23 ms 35768 KB Output is correct
4 Correct 22 ms 35552 KB Output is correct
5 Correct 19 ms 35572 KB Output is correct
6 Correct 19 ms 35796 KB Output is correct
7 Correct 18 ms 35552 KB Output is correct
8 Correct 18 ms 35684 KB Output is correct
9 Correct 20 ms 35984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 35668 KB Output is correct
2 Correct 17 ms 35680 KB Output is correct
3 Correct 23 ms 35768 KB Output is correct
4 Correct 22 ms 35552 KB Output is correct
5 Correct 19 ms 35572 KB Output is correct
6 Correct 19 ms 35796 KB Output is correct
7 Correct 18 ms 35552 KB Output is correct
8 Correct 18 ms 35684 KB Output is correct
9 Correct 20 ms 35984 KB Output is correct
10 Correct 20 ms 35548 KB Output is correct
11 Correct 29 ms 39208 KB Output is correct
12 Correct 44 ms 41884 KB Output is correct
13 Correct 82 ms 57164 KB Output is correct
14 Correct 120 ms 57488 KB Output is correct
15 Correct 165 ms 64872 KB Output is correct
16 Correct 119 ms 56524 KB Output is correct
17 Correct 129 ms 54168 KB Output is correct
18 Correct 48 ms 41904 KB Output is correct
19 Correct 119 ms 57336 KB Output is correct
20 Correct 184 ms 64912 KB Output is correct
21 Correct 148 ms 56396 KB Output is correct
22 Correct 124 ms 53976 KB Output is correct
23 Correct 136 ms 58860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 35668 KB Output is correct
2 Correct 17 ms 35680 KB Output is correct
3 Correct 23 ms 35768 KB Output is correct
4 Correct 22 ms 35552 KB Output is correct
5 Correct 19 ms 35572 KB Output is correct
6 Correct 19 ms 35796 KB Output is correct
7 Correct 18 ms 35552 KB Output is correct
8 Correct 18 ms 35684 KB Output is correct
9 Correct 20 ms 35984 KB Output is correct
10 Correct 20 ms 35548 KB Output is correct
11 Correct 29 ms 39208 KB Output is correct
12 Correct 44 ms 41884 KB Output is correct
13 Correct 82 ms 57164 KB Output is correct
14 Correct 120 ms 57488 KB Output is correct
15 Correct 165 ms 64872 KB Output is correct
16 Correct 119 ms 56524 KB Output is correct
17 Correct 129 ms 54168 KB Output is correct
18 Correct 48 ms 41904 KB Output is correct
19 Correct 119 ms 57336 KB Output is correct
20 Correct 184 ms 64912 KB Output is correct
21 Correct 148 ms 56396 KB Output is correct
22 Correct 124 ms 53976 KB Output is correct
23 Correct 136 ms 58860 KB Output is correct
24 Correct 21 ms 35552 KB Output is correct
25 Correct 367 ms 39248 KB Output is correct
26 Correct 896 ms 42028 KB Output is correct
27 Correct 2542 ms 57192 KB Output is correct
28 Execution timed out 5037 ms 57588 KB Time limit exceeded
29 Halted 0 ms 0 KB -