Submission #348317

# Submission time Handle Problem Language Result Execution time Memory
348317 2021-01-14T15:17:36 Z amunduzbaev Tropical Garden (IOI11_garden) C++14
100 / 100
2835 ms 52320 KB
/** made by amunduzbaev **/
 
#include "garden.h"
#include "gardenlib.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
 
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Pi acos(-1);
#define mod 1e9+7
#define inf 1e18
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii; 
typedef pair<ll, ll> pll; 
typedef vector<ll> vll;
typedef vector<int> vii;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;}
template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;}
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
 
const int NN = 3e5+5;
 
vii edges[NN], gg[NN];
vii cyc;
stack<int> ss;
int goal, oncyc[NN], last, used[NN], len[NN], in[NN]; 
int comp[NN], pos[NN], bup[NN], ddn[NN];
 
void dfs(int u){
	if(used[u] && in[u]){
		oncyc[u] = ++last;
		cyc.pb(u);
		int tmp = 0;
		while(!ss.empty() && ss.top() != u){
			oncyc[ss.top()] = last;
			pos[ss.top()] = ++tmp;
			cyc.pb(ss.top());
			ss.pop();
		}
		len[last] = sz(cyc);
		return;
	}
	if(used[u]) return;
	
	used[u] = 1;
	ss.push(u);
	in[u] = 1;
	
	dfs(bup[u]);
	
	in[u] = 0;
	if(!ss.empty()) ss.pop();
}
 
int tin[NN], tout[NN], tim;
int rr[NN], dd[NN], ddr[NN];
 
void treepr(int u, int root){
	comp[u] = comp[root];
	rr[u] = root;
	tin[u] = ++tim;
	for(auto x:edges[u]){
		if(oncyc[x]) continue;
		ddr[x] = ddr[u]+1;
		treepr(x, root);
	}
	tout[u] = tim;
}
 
bool check(int u, int k, int goal){
	if(comp[u] != comp[goal]) return 0;
	if(oncyc[u] && !oncyc[goal]) return 0;
	
	else if(oncyc[u] && oncyc[goal]){
		k %= len[oncyc[u]];
		return (dd[u] == k);
	}
	
	else if(!oncyc[u] && oncyc[goal]){
		k = k - ddr[u] - dd[rr[u]];
		if(k < 0) return 0;
		return (k % len[oncyc[rr[u]]] == 0);
	}
	
	else if(!oncyc[u] && !oncyc[goal]){
		if(tin[goal] <= tin[u] && tout[goal] >= tout[u]) 
			return (ddr[u] - ddr[goal] == k);
	}
	
	return 0;
}

bool ncheck(int u, int k, int goal){
	if(comp[u] != comp[goal]) return 0;
	if(oncyc[u] && !oncyc[goal]) return 0;
	
	else if(oncyc[u] && oncyc[goal]){
		k %= len[oncyc[u]];
		return (ddn[u] == k);
	}
	
	else if(!oncyc[u] && oncyc[goal]){
		k = k - ddr[u] - ddn[rr[u]];
		if(k < 0) return 0;
		return (k % len[oncyc[rr[u]]] == 0);
	}
	
	else if(!oncyc[u] && !oncyc[goal]){
		if(tin[goal] <= tin[u] && tout[goal] >= tout[u]) 
			return (ddr[u] - ddr[goal] == k);
	}
	
	return 0;
}

 
 
 
void count_routes(int n, int m, int p, int R[][2], int q, int kk[]){
	for(int i=0; i<m; i++){
		int a = R[i][0], b = R[i][1];
		if(sz(gg[a]) < 2) gg[a].pb(b);
		if(sz(gg[b]) < 2) gg[b].pb(a);
	}
	
	memset(rr, -1, sizeof rr);
	memset(bup, -1, sizeof bup);
	memset(dd, -1, sizeof dd);
	memset(ddn, -1, sizeof ddn);
	
	for(int i=0;i<n;i++){
		for(int j=0;j<sz(gg[i]);j++){
			int to = gg[i][j];
			if(gg[to][0] == i && sz(gg[to]) > 1){
				bup[i + j * n] = to+n;
				edges[to+n].pb(i + j*n);
			}else{
				bup[i + j * n] = to;
				edges[to].pb(i + j*n);
			}
		}
	}
	
	goal = p;
	
	for(int i=0;i<2*n;i++){
		if(used[i]) continue;
		while(!ss.empty()) ss.pop();
		cyc.clear();
		dfs(i);
		for(auto x:cyc){
			ddr[x] = 0, comp[x] = oncyc[x];
			treepr(x, x);
		}
	}
	
	if(oncyc[p]){
		int cur = p, dis = 0;
		while(cur != p || dis == 0){
			dd[cur] = (len[oncyc[cur]] - dis), dis++;
			cur = bup[cur];
		}dd[p] = 0;
	}
	
	if(oncyc[p+n]){
		int cur = p+n, dis = 0;
		while(cur != p+n || dis == 0){
			ddn[cur] = (len[oncyc[cur]] - dis), dis++;
			cur = bup[cur];
		}ddn[p+n] = 0;
	}
	
	for(int i=0;i<q;i++){
		int cnt = 0;
		for(int j=0;j<n;j++) cnt += ( check(j, kk[i], p) | ncheck(j, kk[i], p+n) );
		
		answer(cnt);
	}
}
 
/*
 
6 6 0
1 2
0 1
0 3
3 4
4 5
1 5
1
3
2
 
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
1 2
 
*/
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19436 KB Output is correct
2 Correct 12 ms 19328 KB Output is correct
3 Correct 12 ms 19320 KB Output is correct
4 Correct 11 ms 19180 KB Output is correct
5 Correct 14 ms 19180 KB Output is correct
6 Correct 13 ms 19436 KB Output is correct
7 Correct 13 ms 19180 KB Output is correct
8 Correct 14 ms 19308 KB Output is correct
9 Correct 14 ms 19308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19436 KB Output is correct
2 Correct 12 ms 19328 KB Output is correct
3 Correct 12 ms 19320 KB Output is correct
4 Correct 11 ms 19180 KB Output is correct
5 Correct 14 ms 19180 KB Output is correct
6 Correct 13 ms 19436 KB Output is correct
7 Correct 13 ms 19180 KB Output is correct
8 Correct 14 ms 19308 KB Output is correct
9 Correct 14 ms 19308 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 24 ms 22508 KB Output is correct
12 Correct 45 ms 24300 KB Output is correct
13 Correct 77 ms 42596 KB Output is correct
14 Correct 177 ms 35308 KB Output is correct
15 Correct 177 ms 35564 KB Output is correct
16 Correct 133 ms 30956 KB Output is correct
17 Correct 121 ms 29292 KB Output is correct
18 Correct 49 ms 24300 KB Output is correct
19 Correct 165 ms 35308 KB Output is correct
20 Correct 170 ms 35584 KB Output is correct
21 Correct 139 ms 30956 KB Output is correct
22 Correct 132 ms 29332 KB Output is correct
23 Correct 202 ms 37484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19436 KB Output is correct
2 Correct 12 ms 19328 KB Output is correct
3 Correct 12 ms 19320 KB Output is correct
4 Correct 11 ms 19180 KB Output is correct
5 Correct 14 ms 19180 KB Output is correct
6 Correct 13 ms 19436 KB Output is correct
7 Correct 13 ms 19180 KB Output is correct
8 Correct 14 ms 19308 KB Output is correct
9 Correct 14 ms 19308 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 24 ms 22508 KB Output is correct
12 Correct 45 ms 24300 KB Output is correct
13 Correct 77 ms 42596 KB Output is correct
14 Correct 177 ms 35308 KB Output is correct
15 Correct 177 ms 35564 KB Output is correct
16 Correct 133 ms 30956 KB Output is correct
17 Correct 121 ms 29292 KB Output is correct
18 Correct 49 ms 24300 KB Output is correct
19 Correct 165 ms 35308 KB Output is correct
20 Correct 170 ms 35584 KB Output is correct
21 Correct 139 ms 30956 KB Output is correct
22 Correct 132 ms 29332 KB Output is correct
23 Correct 202 ms 37484 KB Output is correct
24 Correct 15 ms 19180 KB Output is correct
25 Correct 141 ms 22508 KB Output is correct
26 Correct 183 ms 24428 KB Output is correct
27 Correct 2578 ms 42724 KB Output is correct
28 Correct 1395 ms 35436 KB Output is correct
29 Correct 2835 ms 35692 KB Output is correct
30 Correct 1730 ms 31084 KB Output is correct
31 Correct 2415 ms 29388 KB Output is correct
32 Correct 818 ms 24044 KB Output is correct
33 Correct 1432 ms 35436 KB Output is correct
34 Correct 2821 ms 35728 KB Output is correct
35 Correct 2438 ms 30956 KB Output is correct
36 Correct 2496 ms 29292 KB Output is correct
37 Correct 1139 ms 37612 KB Output is correct
38 Correct 2597 ms 52320 KB Output is correct