Submission #347559

#TimeUsernameProblemLanguageResultExecution timeMemory
347559Kerim열대 식물원 (Tropical Garden) (IOI11_garden)C++17
0 / 100
11 ms14700 KiB
#include "garden.h"
#include "gardenlib.h"
#include "bits/stdc++.h"
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ppb() pop_back()
#define MAXN 300009
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;
int go[MAXN],C,sz[MAXN],lvl[MAXN],who[MAXN],pos[MAXN];
int vis[MAXN],cycle[MAXN],in[MAXN],ata[MAXN];
vector<int>inv[MAXN],v,adj[MAXN];
int tin[MAXN],tout[MAXN],T;
void dfs(int nd){
	v.pb(nd);
	if(vis[nd]){
		if(in[nd]){
			int tmp=0;
			do{
				cycle[v.back()]=C;
				pos[v.back()]=++tmp;
				sz[C]++;
				v.ppb();
			}while(v.back()!=nd);
		}
		return;
	}vis[nd]=in[nd]=1;
	dfs(go[nd]);
	in[nd]=0;
}	
int dis(int x,int y,int id){
	if(y<=x)
		return x-y;
	return x+sz[id]-y;
}
int baba(int x,int y){
	return (tin[x]<=tin[y] and tout[y]<=tout[x]);	
}
bool check(int src,int dest,int step){
	if(who[src]!=who[dest])
		return 0;
	if(cycle[src] and !cycle[dest])
		return 0;
	if(!cycle[src]){
		if(!cycle[dest]){
			if(lvl[src]-lvl[dest]==step and baba(dest,src))
				return 1;	
		}
		else{
			step-=lvl[src];
			step-=dis(pos[ata[src]],pos[dest],cycle[dest]);	
			if(!step)
				return 1;
		}
	}
	else{
		step-=dis(pos[src],pos[dest],cycle[dest]);
		if(step>=0 and step%sz[cycle[dest]]==0)
			return 1;
	}
	return 0;
}
void dfs1(int nd){
	tin[nd]=++T;
	tr(it,inv[nd])
		if(!cycle[*it]){
			lvl[*it]=lvl[nd]+1;
			ata[*it]=ata[nd];
			who[*it]=who[nd];
			dfs1(*it);
		}
	tout[nd]=T;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
	for(int i=0;i<M;i++){
		int x=R[i][0],y=R[i][1];
		if(adj[x].size()<2)adj[x].push_back(y);
		if(adj[y].size()<2)adj[y].push_back(x);	
	}
	//printf("EDGES\n");
	for(int i=0;i<N;i++)
		for(int j=0;j<(int)adj[i].size();j++){
			int to=adj[i][j];
			if(adj[to][0]==i and adj[to].size()>1){
				go[i+j*N]=to+N;
				//printf("%d %d\n",i+j*N,to+N);
				inv[to+N].pb(i+j*N);	
			}
			else{
				go[i+j*N]=to;
				//printf("%d %d\n",i+j*N,to);
				inv[to].pb(i+j*N);	
			}
		}
	//wr
	for(int i=0;i<2*N;i++)
		if(!vis[i]){
			C++;dfs(i);	
			v.clear();
		}
	for(int i=0;i<2*N;i++)
		if(cycle[i]){
			ata[i]=i;who[i]=cycle[i];
			dfs1(i);
		}
	for(int i=0;i<Q;i++){
		int res=0,ok;
		for(int j=0;j<N;j++){
			ok=check(j,P,G[i]);
			ok|=check(j,P+N,G[i]);
			res+=ok;
		}
		answer(res);
	}
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...