Submission #126576

# Submission time Handle Problem Language Result Execution time Memory
126576 2019-07-08T06:26:16 Z E869120 Cats or Dogs (JOI18_catdog) C++14
38 / 100
67 ms 3448 KB
#include "catdog.h"
#include <vector>
#include <algorithm>
using namespace std;

int N, E[5009], dp[5009][2]; bool used[5009];
vector<int> X[5009];

void initialize(int NN, vector<int> A, vector<int> B) {
	N = NN; for (int i = 1; i <= N; i++) E[i] = 2;
	for (int i = 0; i < A.size(); i++) {
		X[A[i]].push_back(B[i]);
		X[B[i]].push_back(A[i]);
	}
}

void dfs(int pos) {
	used[pos] = true;
	vector<int>G;
	for (int i = 0; i < X[pos].size(); i++) {
		if (used[X[pos][i]] == true) continue;
		G.push_back(X[pos][i]);
		dfs(X[pos][i]);
	}

	// 1. 猫を飼う場合
	for (int pos2 : G) {
		dp[pos][0] += min(dp[pos2][0], dp[pos2][1] + 1);
	}

	// 2. 犬を飼う場合
	for (int pos2 : G) {
		dp[pos][1] += min(dp[pos2][1], dp[pos2][0] + 1);
	}
	
	if (E[pos] == 0) dp[pos][1] = (1 << 30);
	if (E[pos] == 1) dp[pos][0] = (1 << 30);
}

int solve() {
	for (int i = 1; i <= N; i++) { dp[i][0] = 0; dp[i][1] = 0; used[i] = false; }
	dfs(1);
	return min(dp[1][0], dp[1][1]);
}

int cat(int v) {
	E[v] = 0;
	return solve();
}

int dog(int v) {
	E[v] = 1;
	return solve();
}

int neighbor(int v) {
	E[v] = 2;
	return solve();
}

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:11:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < A.size(); i++) {
                  ~~^~~~~~~~~~
catdog.cpp: In function 'void dfs(int)':
catdog.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < X[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 47 ms 516 KB Output is correct
18 Correct 58 ms 536 KB Output is correct
19 Correct 29 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 10 ms 508 KB Output is correct
22 Correct 12 ms 376 KB Output is correct
23 Correct 67 ms 604 KB Output is correct
24 Correct 54 ms 632 KB Output is correct
25 Correct 25 ms 508 KB Output is correct
26 Correct 12 ms 504 KB Output is correct
27 Correct 8 ms 504 KB Output is correct
28 Correct 12 ms 504 KB Output is correct
29 Correct 58 ms 504 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 4 ms 504 KB Output is correct
32 Correct 7 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 47 ms 516 KB Output is correct
18 Correct 58 ms 536 KB Output is correct
19 Correct 29 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 10 ms 508 KB Output is correct
22 Correct 12 ms 376 KB Output is correct
23 Correct 67 ms 604 KB Output is correct
24 Correct 54 ms 632 KB Output is correct
25 Correct 25 ms 508 KB Output is correct
26 Correct 12 ms 504 KB Output is correct
27 Correct 8 ms 504 KB Output is correct
28 Correct 12 ms 504 KB Output is correct
29 Correct 58 ms 504 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 4 ms 504 KB Output is correct
32 Correct 7 ms 376 KB Output is correct
33 Runtime error 35 ms 3448 KB Execution killed with signal 11 (could be triggered by violating memory limits)
34 Halted 0 ms 0 KB -