Submission #1042347

# Submission time Handle Problem Language Result Execution time Memory
1042347 2024-08-02T22:13:01 Z Edu175 Tropical Garden (IOI11_garden) C++17
100 / 100
1382 ms 38500 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,hgfmdg=b;i<hgfmdg;i++)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define imp(v) {for(auto sldhg:v)cout<<sldhg<<" ";cout<<"\n";}
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll MAXN=300005;

vector<ll> g[MAXN];

ll n,p;
ll go(ll u, ll v){
	if(SZ(g[v])==1)return 2*v;
	if(g[v][0]==u)return 2*v+1;
	return 2*v;
}
ll a[MAXN];
ll ds[2][MAXN];
ll zs[2];
void calc(ll w){
	vector<ll>d(2*n,-1),vis(2*n),ord;
	ll e=2*p+w;
	zs[w]=0;
	fore(x,0,2*n)if(!vis[x]){
		vector<ll>p,cyc;
		for(ll y=x;!vis[y];y=a[y]){
			p.pb(y); vis[y]=1;
		}
		ll did=0;
		for(auto x:p){
			did|=x==a[p.back()];
			if(did)cyc.pb(x);
		}
		reverse(ALL(p));
		for(auto i:p)ord.pb(i);
		if(count(ALL(cyc),e)){
			ll m=SZ(cyc);
			ll pos=-1;
			fore(i,0,m)if(cyc[i]==e)pos=i;
			fore(i,0,m){
				d[cyc[i]]=(pos-i+m)%m;
			}
			zs[w]=m;
		}
	}
	for(auto x:ord)if(d[x]==-1){
		if(x==e)d[x]=0;
		else if(d[a[x]]!=-1)d[x]=d[a[x]]+1;
	}
	d[e]=zs[w];
	fore(i,0,2*n)ds[w][i]=d[i];
	// cout<<"calc "<<w<<": "<<zs[w]<<"\n";
	// fore(i,0,2*n)cout<<d[i]<<" ";;cout<<"\n";
	// imp(ord);
}

void count_routes(int N, int m, int P, int R[][2], int q, int qs[])
{
	n=N,p=P;
	fore(i,0,m){
		ll u=R[i][0],v=R[i][1];
		g[u].pb(v);
		g[v].pb(u);
	}
	fore(x,0,n){
		a[2*x]=go(x,g[x][0]);
		if(SZ(g[x])>1)a[2*x+1]=go(x,g[x][1]);
	}
	// fore(x,0,2*n)cout<<a[x]<<" ";;cout<<"\n";
	calc(0); calc(1);
	fore(_,0,q){
		ll k=qs[_],res=0;
		fore(x,0,2*n)if(x%2==0){
			ll flag=0;
			fore(w,0,2){
				ll z=zs[w];
				auto d=ds[w];
				if(d[x]!=-1){
					ll dif=k-d[x];
					flag|=(dif==0||(dif>=0&&z&&dif%z==0));
					// cout<<x<<" "<<w<<": "<<dif<<"\n";
				}
			}
			// if(flag)cout<<x<<" x\n";
			res+=flag;
		}
		// cout<<res<<endl;
		answer(res);
	}
}


# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12724 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12724 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 2 ms 12644 KB Output is correct
11 Correct 9 ms 17608 KB Output is correct
12 Correct 17 ms 19912 KB Output is correct
13 Correct 25 ms 29392 KB Output is correct
14 Correct 53 ms 34428 KB Output is correct
15 Correct 54 ms 35928 KB Output is correct
16 Correct 44 ms 29640 KB Output is correct
17 Correct 41 ms 29636 KB Output is correct
18 Correct 17 ms 19972 KB Output is correct
19 Correct 52 ms 35448 KB Output is correct
20 Correct 56 ms 36264 KB Output is correct
21 Correct 42 ms 29948 KB Output is correct
22 Correct 46 ms 28320 KB Output is correct
23 Correct 58 ms 35504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12724 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 2 ms 12644 KB Output is correct
11 Correct 9 ms 17608 KB Output is correct
12 Correct 17 ms 19912 KB Output is correct
13 Correct 25 ms 29392 KB Output is correct
14 Correct 53 ms 34428 KB Output is correct
15 Correct 54 ms 35928 KB Output is correct
16 Correct 44 ms 29640 KB Output is correct
17 Correct 41 ms 29636 KB Output is correct
18 Correct 17 ms 19972 KB Output is correct
19 Correct 52 ms 35448 KB Output is correct
20 Correct 56 ms 36264 KB Output is correct
21 Correct 42 ms 29948 KB Output is correct
22 Correct 46 ms 28320 KB Output is correct
23 Correct 58 ms 35504 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 117 ms 17740 KB Output is correct
26 Correct 170 ms 19712 KB Output is correct
27 Correct 701 ms 29372 KB Output is correct
28 Correct 895 ms 34848 KB Output is correct
29 Correct 781 ms 36244 KB Output is correct
30 Correct 469 ms 29640 KB Output is correct
31 Correct 455 ms 28576 KB Output is correct
32 Correct 180 ms 19936 KB Output is correct
33 Correct 936 ms 34456 KB Output is correct
34 Correct 709 ms 36596 KB Output is correct
35 Correct 480 ms 29360 KB Output is correct
36 Correct 771 ms 28360 KB Output is correct
37 Correct 794 ms 36500 KB Output is correct
38 Correct 1382 ms 38500 KB Output is correct