제출 #439895

#제출 시각아이디문제언어결과실행 시간메모리
439895dutch열대 식물원 (Tropical Garden) (IOI11_garden)C++17
100 / 100
160 ms25540 KiB
#include <bits/stdc++.h>
using namespace std;
#include "garden.h"
#include "gardenlib.h"
 
const int LIM = 1.5e5, INF = 2e9;
 
int n, cnt[LIM], c, d[2][LIM*2], p, r[2] = {INF, INF};
vector<int> g[LIM*2];
 
void dfs(int u, int dist){
	if(u == p+c*n && dist) return void(r[c] = dist);
	if(u < n) ++d[c][dist];
	for(int v : g[u]) dfs(v, dist + 1);
}
 
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
	n = N, p = P;
	for(int i=0; i<M; ++i){
		for(int j=0; j<2; ++j){
			int u = R[i][j], v = R[i][!j];
			if(cnt[u] == 0) g[v + (!cnt[v])*n].push_back(u);
			if(cnt[u] == 1) g[v + (!cnt[v])*n].push_back(u+n);
		}
		for(int j=0; j<2; ++j) ++cnt[R[i][j]];
	}
	for(int u=0; u<n; ++u) if(cnt[u] < 2){
		for(int v : g[u+n]) g[u].push_back(v);
		g[u+n].clear();
	}
	for(c=0; c<2; ++c) dfs(p+c*n, 0);
 
	for(int j=0; j<2; ++j){
		for(int i=r[j]; i<n+n; ++i){
			d[j][i] += d[j][i-r[j]];
		}
	}
	n = 2*n - 1;
	int l[2];
	for(int j=0; j<2; ++j) if(r[j] < n){
		l[j] = r[j] * (n / r[j]);
		while(l[j] <= n) l[j] += r[j];
	}
 
	for(int i=0; i<Q; ++i){
		int res = 0;
		for(int j=0; j<2; ++j){
			if(G[i] > n && r[j] <= n){
				int x = l[j] + (G[i] % r[j]);
				while(x > n) x -= r[j];
				res += d[j][x];
			}
			if(G[i] <= n){
				res += d[j][G[i]];
			}
		}
		answer(res);
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...