답안 #216227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
216227 2020-03-26T23:50:28 Z peuch 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
49 / 100
43 ms 17400 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 150010;

int beaulty[MAXN][2];
int ar[2 * MAXN], in[2 * MAXN], dist[2 * MAXN], marc[2 * MAXN], mod[2 * MAXN];
vector<int> rev[2 * MAXN];
int cycleSize[2];
int n, p;

void bfs(int ini);
void getCycle();

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{	
	p = P;
	n = N;
	for(int i = 0; i < N; i++){
		ar[2 * i] = -1;
		ar[2 * i + 1] = -1;
	}
	for(int i = 0; i < M; i++){
		if(ar[2 * R[i][0]] == -1) ar[2 * R[i][0]] = i, beaulty[i][0] = 1;
		else if(ar[2 * R[i][0] + 1] == -1) ar[2 * R[i][0] + 1] = i, beaulty[i][0] = 2;
		if(ar[2 * R[i][1]] == -1) ar[2 * R[i][1]] = i, beaulty[i][1] = 1;
		else if(ar[2 * R[i][1] + 1] == -1) ar[2 * R[i][1] + 1] = i, beaulty[i][1] = 2;
	}
	for(int i = 0; i < M; i++){
		// printf("%d -- %d (%d)\n", R[i][0], R[i][1], i);
		// printf("%d -- %d\n", beaulty[i][0], beaulty[i][1]);
		if(beaulty[i][0] == 1 && beaulty[i][1] == 1){
			ar[2 * R[i][0]] = 2 * R[i][1] + 1;
			ar[2 * R[i][1]] = 2 * R[i][0] + 1;
		}
		else if(beaulty[i][0] == 2 && beaulty[i][1] == 1){
			ar[2 * R[i][0] + 1] = 2 * R[i][1] + 1;
			ar[2 * R[i][1]] = 2 * R[i][0];
		}
		else if(beaulty[i][0] == 1 && beaulty[i][1] == 2){
			ar[2 * R[i][0]] = 2 * R[i][1];
			ar[2 * R[i][1] + 1] = 2 * R[i][0] + 1;
		}
		else{
			if(beaulty[i][0] > 0)
				ar[2 * R[i][0] + (beaulty[i][0]) - 1] = 2 * R[i][1];
			if(beaulty[i][1] > 0)
				ar[2 * R[i][1] + (beaulty[i][1]) - 1] = 2 * R[i][0];
		}
	}
	for(int i = 0; i < N; i++){
		if(ar[2 * i] == -1)
			ar[2 * i] = ar[2 * i + 1]; 
		if(ar[2 * i + 1] == -1)
			ar[2 * i + 1] = ar[2 * i];
	}
	for(int i = 0; i < N; i++){
		rev[ar[2 * i]].push_back(2 * i);
		rev[ar[2 * i + 1]].push_back(2 * i + 1);
	}
	for(int i = 0; i < N; i++)
		in[2 * i] = rev[2 * i].size(), in[2 * i + 1] = rev[2 * i + 1].size();
	for(int i = 0; i < N; i++){
		// printf("%d.0 %d.%d\n%d.1 %d.%d\n", i, ar[2 * i] / 2, ar[2 * i] % 2, i, ar[2 * i + 1] / 2, ar[2 * i + 1] % 2);
	}
	getCycle();
	bfs(P);
	// for(int i = 0; i < N; i++){
	// 	printf("%d\n", dist[2 * i]);
	// }
	//printf("\n");
	// for(int i = 0; i < 2 * n; i++){
	// 	if(!marc[i]) printf("%d.%d (%d.%d) ", i / 2, i % 2, ar[i] / 2, ar[i] % 2);
	// }
	// printf("\n");
	for(int i = 0; i < Q; i++){
		int ans = 0;
		for(int j = 0; j < N; j++){
			if(dist[2 * j] == -1) continue;
			if(dist[2 * j] <= G[i] && dist[2 * j] % mod[2 * j] == G[i] % mod[2 * j]) ans++;
		}
		answer(ans);
	}
}

void bfs(int ini){
	queue<int> fila;
	for(int i = 0; i < n; i++){
		dist[2 * i] = -1;
		dist[2 * i + 1] = -1;
	}
	dist[2 * ini] = 0;
	dist[2 * ini + 1] = 0;
	mod[2 * ini] = cycleSize[0];
	mod[2 * ini + 1] = cycleSize[1];
	fila.push(2 * ini);
	fila.push(2 * ini + 1);
	while(!fila.empty()){
		int cur = fila.front();
		// printf("\t%d.%d\n", cur / 2, cur % 2);
		fila.pop();
		for(int i = 0; i < rev[cur].size(); i++){
			int viz = rev[cur][i];
			if(dist[viz] != -1) continue;
			fila.push(viz);
			dist[viz] = dist[cur] + 1;
			mod[viz] = mod[cur];
		}
	}
}

void getCycle(){
	queue<int> fila;
	for(int i = 0; i < n; i++){
		if(in[2 * i] == 0) fila.push(2 * i);
		if(in[2 * i + 1] == 0) fila.push(2 * i + 1);
	}
	while(!fila.empty()){
		int cur = fila.front();
		marc[cur] = 1;
		fila.pop();
		int viz = ar[cur];
		if(marc[viz]) continue;
		in[viz]--;
		if(in[viz] == 0) fila.push(viz);
	}
	if(!marc[2 * p + 1]){
		int cur = 2 * p + 1;
		while(marc[cur] == 0){
			marc[cur] = 1;
			cycleSize[1]++;
			cur = ar[cur];
		}
	}
	else cycleSize[1] = 10 * MAXN;
	if(!marc[2 * p]){
		int cur = 2 * p;
		while(marc[cur] == 0){
			marc[cur] = 1;
			cycleSize[0]++;
			cur = ar[cur];
		}
	}
	else cycleSize[0] = 10 * MAXN;
}

Compilation message

garden.cpp: In function 'void bfs(int)':
garden.cpp:104:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < rev[cur].size(); i++){
                  ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 8 ms 7552 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 8 ms 7552 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 8 ms 7424 KB Output is correct
11 Correct 17 ms 9472 KB Output is correct
12 Correct 31 ms 11128 KB Output is correct
13 Incorrect 43 ms 17400 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7552 KB Output is correct
2 Correct 8 ms 7552 KB Output is correct
3 Correct 8 ms 7552 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 8 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 10 ms 7552 KB Output is correct
10 Correct 8 ms 7424 KB Output is correct
11 Correct 17 ms 9472 KB Output is correct
12 Correct 31 ms 11128 KB Output is correct
13 Incorrect 43 ms 17400 KB Output isn't correct
14 Halted 0 ms 0 KB -