답안 #415334

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415334 2021-05-31T22:02:36 Z CSQ31 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
3062 ms 78440 KB
#include "garden.h"
#include <bits/stdc++.h>
#include "gardenlib.h"
using namespace std;
#define sz(a) (int)(a.size())
#define pb push_back
#define se second
typedef vector<vector<int>> vii;
typedef pair<int,int> pii;
typedef long long int ll;
typedef vector<vector<ll>> VII;
const int MAXN = 4e5+5;
int nxt[MAXN][31];
vector<vector<pii>> g1(MAXN);
vii g2(MAXN);
void count_routes(int n, int m, int p, int r[][2], int q, int g[])
{
	for(int i=0;i<m;i++){
		g1[r[i][0]].pb({i,r[i][1]});
		g1[r[i][1]].pb({i,r[i][0]});
	}
	for(int i=0;i<n;i++)sort(g1[i].begin(),g1[i].end());
	for(int i=0;i<n;i++){
		int to = g1[i][0].se;
		if(g1[to][0].se != i){
			nxt[i][0] = to; //not beautiful
			g2[to].pb(i);
		}
		else {
			nxt[i][0] = to+n;
			g2[to+n].pb(i);
		}
		
		to = (sz(g1[i]) > 1)?g1[i][1].se:g1[i][0].se;
		if(g1[to][0].se != i){
			nxt[i+n][0] = to; //not beautiful
			g2[to].pb(i+n);
		}
		else {
			nxt[i+n][0] = to+n;
			g2[to+n].pb(i+n);
		}
	}
	vector<int>cycle(2*n),vis(2*n);
	int cur = p;
	while(!vis[cur]){
		vis[cur] = 1;
		cur = nxt[cur][0];
	}
	int len1=-1,len2=-1;
	int len = 0;
	while(!cycle[cur]){
		cycle[cur] = 1;
		len++;
		cur = nxt[cur][0];
	}
	if(cycle[p])len1 = len;
	cur = p+n;
	cycle.assign(2*n,0);
	vis.assign(2*n,0);
	while(!vis[cur]){
		vis[cur] = 1;
		cur = nxt[cur][0];
	}
	len = 0;
	while(!cycle[cur]){
		cycle[cur] = 1;
		len++;
		cur = nxt[cur][0];
	}
	if(cycle[p+n])len2 = len;	
	vii d(2,vector<int>(2*n,-1));
	queue<int>qq;
	qq.push(p);
	d[0][p] = 0;
	vis.assign(2*n,0);
	vis[p] = 1;
	while(!qq.empty()){
		int v = qq.front();
		qq.pop();
		for(int x:g2[v]){
			if(vis[x])continue;
			vis[x] =1;
			d[0][x] = d[0][v]+1;
			qq.push(x);
		}
	}
	d[1][p+n] = 0;
	qq.push(p+n);
	vis.assign(2*n,0);
	vis[p+n] = 1;
	while(!qq.empty()){
		int v = qq.front();
		qq.pop();
		for(int x:g2[v]){
			if(vis[x])continue;
			vis[x] =1;
			d[1][x] = d[1][v]+1;
			qq.push(x);
		}
	}
	
	for(int i=0;i<q;i++){
		int ans = 0;
		for(int j=0;j<n;j++){
			bool ok=0;
			if(d[0][j] != -1 && ((len1 != -1 && g[i] >= d[0][j] && (g[i]-d[0][j])%len1 == 0) ||  d[0][j] == g[i]))ok = 1;
			if(d[1][j] != -1 && ((len2 != -1 && g[i] >= d[1][j] && (g[i]-d[1][j])%len2 == 0) ||  d[1][j] == g[i]))ok = 1;
			ans+=ok;
		}			
		answer(ans);
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19404 KB Output is correct
2 Correct 15 ms 19440 KB Output is correct
3 Correct 14 ms 19404 KB Output is correct
4 Correct 14 ms 19020 KB Output is correct
5 Correct 15 ms 19124 KB Output is correct
6 Correct 13 ms 19532 KB Output is correct
7 Correct 14 ms 19128 KB Output is correct
8 Correct 12 ms 19404 KB Output is correct
9 Correct 15 ms 19664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19404 KB Output is correct
2 Correct 15 ms 19440 KB Output is correct
3 Correct 14 ms 19404 KB Output is correct
4 Correct 14 ms 19020 KB Output is correct
5 Correct 15 ms 19124 KB Output is correct
6 Correct 13 ms 19532 KB Output is correct
7 Correct 14 ms 19128 KB Output is correct
8 Correct 12 ms 19404 KB Output is correct
9 Correct 15 ms 19664 KB Output is correct
10 Correct 12 ms 19148 KB Output is correct
11 Correct 29 ms 27368 KB Output is correct
12 Correct 60 ms 33100 KB Output is correct
13 Correct 89 ms 51712 KB Output is correct
14 Correct 161 ms 67884 KB Output is correct
15 Correct 257 ms 68672 KB Output is correct
16 Correct 213 ms 53444 KB Output is correct
17 Correct 167 ms 48580 KB Output is correct
18 Correct 55 ms 33092 KB Output is correct
19 Correct 222 ms 67920 KB Output is correct
20 Correct 211 ms 68660 KB Output is correct
21 Correct 151 ms 53440 KB Output is correct
22 Correct 131 ms 48580 KB Output is correct
23 Correct 200 ms 73144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19404 KB Output is correct
2 Correct 15 ms 19440 KB Output is correct
3 Correct 14 ms 19404 KB Output is correct
4 Correct 14 ms 19020 KB Output is correct
5 Correct 15 ms 19124 KB Output is correct
6 Correct 13 ms 19532 KB Output is correct
7 Correct 14 ms 19128 KB Output is correct
8 Correct 12 ms 19404 KB Output is correct
9 Correct 15 ms 19664 KB Output is correct
10 Correct 12 ms 19148 KB Output is correct
11 Correct 29 ms 27368 KB Output is correct
12 Correct 60 ms 33100 KB Output is correct
13 Correct 89 ms 51712 KB Output is correct
14 Correct 161 ms 67884 KB Output is correct
15 Correct 257 ms 68672 KB Output is correct
16 Correct 213 ms 53444 KB Output is correct
17 Correct 167 ms 48580 KB Output is correct
18 Correct 55 ms 33092 KB Output is correct
19 Correct 222 ms 67920 KB Output is correct
20 Correct 211 ms 68660 KB Output is correct
21 Correct 151 ms 53440 KB Output is correct
22 Correct 131 ms 48580 KB Output is correct
23 Correct 200 ms 73144 KB Output is correct
24 Correct 13 ms 19148 KB Output is correct
25 Correct 126 ms 27508 KB Output is correct
26 Correct 180 ms 33692 KB Output is correct
27 Correct 2575 ms 52676 KB Output is correct
28 Correct 1214 ms 69720 KB Output is correct
29 Correct 3062 ms 70388 KB Output is correct
30 Correct 1717 ms 55108 KB Output is correct
31 Correct 1707 ms 50368 KB Output is correct
32 Correct 210 ms 33732 KB Output is correct
33 Correct 1186 ms 69604 KB Output is correct
34 Correct 2971 ms 70336 KB Output is correct
35 Correct 1825 ms 55028 KB Output is correct
36 Correct 1731 ms 50176 KB Output is correct
37 Correct 913 ms 74968 KB Output is correct
38 Correct 2348 ms 78440 KB Output is correct