Submission #111615

# Submission time Handle Problem Language Result Execution time Memory
111615 2019-05-15T17:46:30 Z gs14004 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 8824 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 sz[MAXN], chn[MAXN], csz[MAXN], dfn[MAXN], par[MAXN], piv;

int slack[MAXN][3];

vector<int> line_query(int s, int e){
	vector<int> ans(3);
	for(int i = e; i >= s; i--){
		for(int j = 0; j < 3; j++){
			ans[j] += slack[i][j];
		}
		vector<int> nxt(3);
		nxt[0] += min({ans[0] + (a[i] ? inf : 0), ans[1] + 1 + (a[i] == 2 ? inf : 0), ans[2] + 1 + (a[i] == 1 ? inf : 0)});
		nxt[1] += min({ans[0] + (a[i] ? inf : 0), ans[1] + 0 + (a[i] == 2 ? inf : 0), ans[2] + 1 + (a[i] == 1 ? inf : 0)});
		nxt[2] += min({ans[0] + (a[i] ? inf : 0), ans[1] + 1 + (a[i] == 2 ? inf : 0), ans[2] + 0 + (a[i] == 1 ? inf : 0)});
		ans = nxt;
	}
	return ans;
}

void Do(int v, int w){
	vector<int> pth;
	for(int i = v; i; i = par[chn[i]]){
		pth.push_back(i);
	}
	reverse(pth.begin(), pth.end());
	for(auto &i : pth){
		auto queryV = line_query(dfn[chn[i]], dfn[chn[i]] + csz[chn[i]] - 1);
		int parV = par[chn[i]];
		parV = dfn[parV];
		slack[parV][0] -= queryV[0];
		slack[parV][1] -= queryV[1];
		slack[parV][2] -= queryV[2];
	}
	a[dfn[v]] = w;
	reverse(pth.begin(), pth.end());
	for(auto &i : pth){
		auto queryV = line_query(dfn[chn[i]], dfn[chn[i]] + csz[chn[i]] - 1);
		int parV = par[chn[i]];
		parV = dfn[parV];
		slack[parV][0] += queryV[0];
		slack[parV][1] += queryV[1];
		slack[parV][2] += queryV[2];
	}
}

int cat(int v) {
	Do(v, 1);
	return *min_element(slack[0], slack[0] + 3);
}

int dog(int v) {
	Do(v, 2);
	return *min_element(slack[0], slack[0] + 3);
}

int neighbor(int v) {
	Do(v, 0);
	return *min_element(slack[0], slack[0] + 3);
}

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		gph[i].erase(find(gph[i].begin(), gph[i].end(), x));
		par[i] = x;
		dfs(i);
		sz[x] += sz[i];
	}
	sort(gph[x].begin(), gph[x].end(), [&](const int &a, const int &b){
		return sz[a] > sz[b];
	});
}

void hld(int x){
	dfn[x] = ++piv;
	csz[chn[x]]++;
	if(gph[x].empty()) return;
	chn[gph[x][0]] = chn[x];
	hld(gph[x][0]);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i]] = gph[x][i];
		hld(gph[x][i]);
		auto q = line_query(dfn[gph[x][i]], dfn[gph[x][i]] + csz[gph[x][i]] - 1);
		slack[dfn[x]][0] += q[0];
		slack[dfn[x]][1] += q[1];
		slack[dfn[x]][2] += q[2];
	}
}

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);
	chn[1] = 1;
	hld(1);
	auto q = line_query(dfn[1], dfn[1] + csz[1] - 1);
	slack[0][0] = q[0];
	slack[0][1] = q[1];
	slack[0][2] = q[2];
}


Compilation message

catdog.cpp: In function 'void hld(int)':
catdog.cpp:88:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1; i<gph[x].size(); i++){
               ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 3 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 3 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2560 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 3 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 3 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2560 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 10 ms 2816 KB Output is correct
18 Correct 9 ms 2816 KB Output is correct
19 Correct 7 ms 2816 KB Output is correct
20 Correct 5 ms 2816 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 5 ms 2816 KB Output is correct
23 Correct 13 ms 2816 KB Output is correct
24 Correct 10 ms 2816 KB Output is correct
25 Correct 8 ms 2688 KB Output is correct
26 Correct 5 ms 2688 KB Output is correct
27 Correct 11 ms 2688 KB Output is correct
28 Correct 15 ms 2816 KB Output is correct
29 Correct 74 ms 2816 KB Output is correct
30 Correct 4 ms 2688 KB Output is correct
31 Correct 4 ms 2816 KB Output is correct
32 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
3 Correct 3 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 3 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2560 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 10 ms 2816 KB Output is correct
18 Correct 9 ms 2816 KB Output is correct
19 Correct 7 ms 2816 KB Output is correct
20 Correct 5 ms 2816 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 5 ms 2816 KB Output is correct
23 Correct 13 ms 2816 KB Output is correct
24 Correct 10 ms 2816 KB Output is correct
25 Correct 8 ms 2688 KB Output is correct
26 Correct 5 ms 2688 KB Output is correct
27 Correct 11 ms 2688 KB Output is correct
28 Correct 15 ms 2816 KB Output is correct
29 Correct 74 ms 2816 KB Output is correct
30 Correct 4 ms 2688 KB Output is correct
31 Correct 4 ms 2816 KB Output is correct
32 Correct 4 ms 2688 KB Output is correct
33 Correct 2032 ms 8788 KB Output is correct
34 Correct 684 ms 8824 KB Output is correct
35 Execution timed out 3044 ms 7152 KB Time limit exceeded
36 Halted 0 ms 0 KB -