Submission #110638

# Submission time Handle Problem Language Result Execution time Memory
110638 2019-05-11T18:19:40 Z gs14004 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 10472 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int inf = 1e9;

int n, a[MAXN];
vector<int> gph[MAXN];
int dp[MAXN][3], par[MAXN];

void cons(int v){
	memset(dp[v], 0, sizeof(dp[v]));
	for(auto &i : gph[v]){
		dp[v][0] += min({dp[i][0] + (a[i] ? inf : 0), dp[i][1] + 1 + (a[i] == 2 ? inf : 0), dp[i][2] + 1 + (a[i] == 1 ? inf : 0)});
		dp[v][1] += min({dp[i][0] + (a[i] ? inf : 0), dp[i][1] + (a[i] == 2 ? inf : 0), dp[i][2] + 1 + (a[i] == 1 ? inf : 0)});
		dp[v][2] += min({dp[i][0] + (a[i] ? inf : 0), dp[i][1] + 1 + (a[i] == 2 ? inf : 0), dp[i][2] + (a[i] == 1 ? inf : 0)});
	}
}

int cat(int v) {
	a[v] = 1;
	while(v){
		cons(v);
		v = par[v];
	}
	if(a[1]) return dp[1][a[1]];
	return min({dp[1][0], dp[1][1], dp[1][2]});
}

int dog(int v) {
	a[v] = 2;
	while(v){
		cons(v);
		v = par[v];
	}
	if(a[1]) return dp[1][a[1]];
	return min({dp[1][0], dp[1][1], dp[1][2]});
}

int neighbor(int v) {
	a[v] = 0;
	while(v){
		cons(v);
		v = par[v];
	}
	if(a[1]) return dp[1][a[1]];
	return min({dp[1][0], dp[1][1], dp[1][2]});
}

void dfs(int x){
	for(auto &i : gph[x]){
		gph[i].erase(find(gph[i].begin(), gph[i].end(), x));
		par[i] = x;
		dfs(i);
	}
	cons(x);
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
	n = N;
	for(int i=0; i<n-1; i++){
		gph[A[i]].push_back(B[i]);
		gph[B[i]].push_back(A[i]);
	}
	dfs(1);
}


# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 7 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 5 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 4 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 7 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 5 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 4 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 5 ms 2688 KB Output is correct
19 Correct 4 ms 2688 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 5 ms 2688 KB Output is correct
22 Correct 4 ms 2688 KB Output is correct
23 Correct 5 ms 2688 KB Output is correct
24 Correct 9 ms 2688 KB Output is correct
25 Correct 5 ms 2688 KB Output is correct
26 Correct 4 ms 2688 KB Output is correct
27 Correct 5 ms 2688 KB Output is correct
28 Correct 5 ms 2816 KB Output is correct
29 Correct 13 ms 2816 KB Output is correct
30 Correct 6 ms 2688 KB Output is correct
31 Correct 5 ms 2816 KB Output is correct
32 Correct 6 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 7 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 5 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 4 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 5 ms 2688 KB Output is correct
19 Correct 4 ms 2688 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 5 ms 2688 KB Output is correct
22 Correct 4 ms 2688 KB Output is correct
23 Correct 5 ms 2688 KB Output is correct
24 Correct 9 ms 2688 KB Output is correct
25 Correct 5 ms 2688 KB Output is correct
26 Correct 4 ms 2688 KB Output is correct
27 Correct 5 ms 2688 KB Output is correct
28 Correct 5 ms 2816 KB Output is correct
29 Correct 13 ms 2816 KB Output is correct
30 Correct 6 ms 2688 KB Output is correct
31 Correct 5 ms 2816 KB Output is correct
32 Correct 6 ms 2688 KB Output is correct
33 Correct 704 ms 6964 KB Output is correct
34 Correct 210 ms 6904 KB Output is correct
35 Correct 1041 ms 6100 KB Output is correct
36 Correct 1161 ms 9660 KB Output is correct
37 Correct 20 ms 4736 KB Output is correct
38 Correct 1285 ms 10344 KB Output is correct
39 Correct 1398 ms 10340 KB Output is correct
40 Correct 1849 ms 10344 KB Output is correct
41 Correct 1652 ms 10340 KB Output is correct
42 Correct 2348 ms 10472 KB Output is correct
43 Correct 1737 ms 10400 KB Output is correct
44 Correct 1849 ms 10340 KB Output is correct
45 Execution timed out 3099 ms 9820 KB Time limit exceeded
46 Halted 0 ms 0 KB -