답안 #248975

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248975 2020-07-13T19:49:03 Z eohomegrownapps 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
2780 ms 24588 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
//answer(N) -- query

int n;
int INF = 1e9;
int p;
vector<vector<int>> adjlist;

vector<vector<pair<int,int>>> dp; 
//node, comes from beautiful
//until k, type of k visit
//-1 - never cycles, 0 - returns unblocked, 1 - returns blocked

pair<int,int> genk(int node, bool blocked, bool start){
	//cout<<node<<" "<<blocked<<'\n';
	if (node==p&&!start){
		//we've found p
		return {0,blocked};
	}
	if (dp[node][blocked].first!=-1){
		return dp[node][blocked];
	}
	if (dp[node][blocked].second==true){
		//we've reached a cycle
		return dp[node][blocked]={INF,-1};
	}
	dp[node][blocked].second = true; //to check for cycles
	int next = adjlist[node][0];
	if (adjlist[node].size()>1&&blocked){
		next = adjlist[node][1];
	}
	bool isBlocked = adjlist[next][0]==node;
	pair<int,int> ans = genk(next,isBlocked,false);
	if (ans.first==INF){
		return dp[node][blocked]={INF,-1};
	} else {
		return dp[node][blocked]={ans.first+1,ans.second};
	}
}

pair<int,int> followpath(int node, bool blocked){
	if (node==p){
		return {0,blocked};
	}
	if (dp[node][blocked].first!=-1){
		return dp[node][blocked];
	}
	if (dp[node][blocked].second==true){
		//we've reached a bad cycle
		return dp[node][blocked]={INF,false};
	}
	dp[node][blocked].second = true; //to check for cycles
	int next = adjlist[node][0];
	if (adjlist[node].size()>1&&blocked){
		next = adjlist[node][1];
	}
	bool isBlocked = adjlist[next][0]==node;
	pair<int,int> ans = followpath(next,isBlocked);
	if (ans.first==INF){
		return dp[node][blocked]={INF,false};
	} else {
		return dp[node][blocked]={ans.first+1,ans.second};
	}
}

bool routeExists(int i, int val){
	if (dp[i][0].first>=INF){
		return false;
	}
	if (dp[i][0].first>val){
		return false;
	}
	/*if (dp[i][0].first==val){
		return true;
	}*/
	bool b = dp[i][0].second;
	if (dp[p][b].first>=INF){
		//X->?
		return val==dp[i][0].first;
	} else if (dp[p][b].second==b){
		//X->X
		val-=dp[i][0].first;
		return val%dp[p][b].first==0;
	} else {
		val-=dp[i][0].first;
		//X->Y
		if (val==0){return true;}
		if (dp[p][!b].first>=INF){
			//X->Y->?
			return val==dp[p][b].first;
		} else if (dp[p][!b].second==!b){
			//X->Y->Y
			val-=dp[p][b].first;
			return val%dp[p][!b].first==0;
		} else {
			//X->Y->X
			val%=(dp[p][b].first+dp[p][!b].first);
			return (val==0||val==dp[p][b].first);
		}
	}
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
	n=N;p=P;
	adjlist.resize(n);
	dp.resize(n,vector<pair<int,int>>(2,{-1,false}));
	for (int i = 0; i<M; i++){
		adjlist[R[i][0]].push_back(R[i][1]);
		adjlist[R[i][1]].push_back(R[i][0]);
	}
	//cout<<"gen\n";
	genk(p,false,true);
	//cout<<dp[p][0].first<<'\n';
	//cout<<"gen\n";
	genk(p,true,true);
	//cout<<dp[p][1].first<<'\n';
	for (int i = 0; i<n; i++){
		for (int blocked = 0; blocked<2; blocked++){
			if (dp[i][blocked].first==-1){
				followpath(i,blocked);
			}
		}
	}

	//todo: check for case where inf
	for (int q = 0; q<Q; q++){
		int cnt = 0;
		int val = G[q];
		for (int i = 0; i<n; i++){
			if (routeExists(i,val)){
				cnt++;
			}
		}
		answer(cnt);
	}
}


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 288 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 288 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 12 ms 3328 KB Output is correct
12 Correct 26 ms 5760 KB Output is correct
13 Correct 44 ms 16632 KB Output is correct
14 Correct 100 ms 18168 KB Output is correct
15 Correct 115 ms 18552 KB Output is correct
16 Correct 83 ms 13560 KB Output is correct
17 Correct 77 ms 12024 KB Output is correct
18 Correct 26 ms 5624 KB Output is correct
19 Correct 108 ms 18168 KB Output is correct
20 Correct 110 ms 18428 KB Output is correct
21 Correct 87 ms 13432 KB Output is correct
22 Correct 82 ms 12028 KB Output is correct
23 Correct 106 ms 20088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 288 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 12 ms 3328 KB Output is correct
12 Correct 26 ms 5760 KB Output is correct
13 Correct 44 ms 16632 KB Output is correct
14 Correct 100 ms 18168 KB Output is correct
15 Correct 115 ms 18552 KB Output is correct
16 Correct 83 ms 13560 KB Output is correct
17 Correct 77 ms 12024 KB Output is correct
18 Correct 26 ms 5624 KB Output is correct
19 Correct 108 ms 18168 KB Output is correct
20 Correct 110 ms 18428 KB Output is correct
21 Correct 87 ms 13432 KB Output is correct
22 Correct 82 ms 12028 KB Output is correct
23 Correct 106 ms 20088 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 176 ms 3576 KB Output is correct
26 Correct 225 ms 5760 KB Output is correct
27 Correct 1340 ms 16888 KB Output is correct
28 Correct 1524 ms 18224 KB Output is correct
29 Correct 2780 ms 18552 KB Output is correct
30 Correct 1659 ms 13816 KB Output is correct
31 Correct 1575 ms 12164 KB Output is correct
32 Correct 243 ms 5624 KB Output is correct
33 Correct 1509 ms 18220 KB Output is correct
34 Correct 2769 ms 18560 KB Output is correct
35 Correct 1752 ms 13688 KB Output is correct
36 Correct 1790 ms 12152 KB Output is correct
37 Correct 1308 ms 20216 KB Output is correct
38 Correct 2307 ms 24588 KB Output is correct