답안 #111616

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
111616 2019-05-15T17:47:23 Z gs14004 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 7976 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]);
	}
}

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);
}


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++){
               ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 3 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 3 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 3 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 3 ms 2688 KB Output is correct
15 Correct 3 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 3 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 3 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 3 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 3 ms 2688 KB Output is correct
15 Correct 3 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 8 ms 2944 KB Output is correct
18 Correct 8 ms 2816 KB Output is correct
19 Correct 6 ms 2816 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 11 ms 2816 KB Output is correct
24 Correct 12 ms 2816 KB Output is correct
25 Correct 7 ms 2816 KB Output is correct
26 Correct 5 ms 2816 KB Output is correct
27 Correct 11 ms 2816 KB Output is correct
28 Correct 16 ms 2816 KB Output is correct
29 Correct 81 ms 2856 KB Output is correct
30 Correct 5 ms 2816 KB Output is correct
31 Correct 5 ms 2816 KB Output is correct
32 Correct 6 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 3 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 3 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 3 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 3 ms 2688 KB Output is correct
15 Correct 3 ms 2688 KB Output is correct
16 Correct 4 ms 2688 KB Output is correct
17 Correct 8 ms 2944 KB Output is correct
18 Correct 8 ms 2816 KB Output is correct
19 Correct 6 ms 2816 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 11 ms 2816 KB Output is correct
24 Correct 12 ms 2816 KB Output is correct
25 Correct 7 ms 2816 KB Output is correct
26 Correct 5 ms 2816 KB Output is correct
27 Correct 11 ms 2816 KB Output is correct
28 Correct 16 ms 2816 KB Output is correct
29 Correct 81 ms 2856 KB Output is correct
30 Correct 5 ms 2816 KB Output is correct
31 Correct 5 ms 2816 KB Output is correct
32 Correct 6 ms 2688 KB Output is correct
33 Correct 2052 ms 7776 KB Output is correct
34 Correct 662 ms 7976 KB Output is correct
35 Execution timed out 3036 ms 6100 KB Time limit exceeded
36 Halted 0 ms 0 KB -