답안 #294228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
294228 2020-09-08T17:30:59 Z spdskatr Spring cleaning (CEOI20_cleaning) C++14
100 / 100
259 ms 26992 KB
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

int N, Q, leaf[100005], lvs[100005], depth[100005], evens[100005], jp[18][100005], rawtot, seen[100005];
int sz[100005], ind[100005], offset[100005], cnt, newans;
vector<int> graph[100005], g2[100005];
vector<int> nl, newnodes, comps;

void dfs1(int x, int p) {
	//printf("dfs1(%d, %d)\n", x, p);
	leaf[x] = 1;
	ind[x] = cnt++;
	sz[x] = 1;
	jp[0][x] = p;
	for (int v : graph[x]) {
		if (v != p) {
			leaf[x] = 0;
			depth[v] = depth[x] + 1;
			dfs1(v, x);
			lvs[x] += lvs[v];
			sz[x] += sz[v];
		}
	}
	if (x == 1 && graph[x].size() <= 1) leaf[x] = 1;
	lvs[x] += leaf[x];
}

void dfs2(int x, int p) {
	if (x != 1) rawtot += 1 + ((lvs[x] % 2) ^ 1);
	evens[x] += ((lvs[x] % 2) ^ 1);
	for (int v : graph[x]) {
		if (v != p) {
			evens[v] = evens[x];
			dfs2(v, x);
		}
	}
}

void dfs3(int x, int p) {
	//printf("dfs3(%d, %d)\n", x, p);
	for (int v : g2[x]) {
		dfs3(v, x);
		offset[x] += offset[v];
	}
	if (offset[x] & 1) {
		newans += depth[x] - depth[p] - 2 * (evens[x] - evens[p]);
	}
}

int isanc(int a, int b) {
	return ind[a] <= ind[b] && ind[a] + sz[a] > ind[b];
}

int lca(int u, int v) {
	if (isanc(u, v)) return u;
	if (isanc(v, u)) return v;
	int cur = u;
	for (int i = 17; i >= 0; i--) {
		if (!isanc(jp[i][cur], v)) cur = jp[i][cur];
	}
	return jp[0][cur];
}

int main() {
	scanf("%d %d", &N, &Q);
	for (int i = 0; i < N-1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		graph[u].push_back(v);
		graph[v].push_back(u);
	}
	dfs1(1, 1);
	dfs2(1, 1);
	for (int b = 1; b < 18; b++) {
		for (int i = 1; i <= N; i++) {
			jp[b][i] = jp[b-1][jp[b-1][i]];
		}
	}
	for (int q = 0; q < Q; q++) {
		//printf("==========\n");
		int d;
		scanf("%d", &d);
		nl.clear();
		for (int i = 0; i < d; i++) {
			int a;
			scanf("%d", &a);
			nl.push_back(a);
		}
		int createdleaves = 0;
		sort(nl.begin(), nl.end(), [](int l, int r){
			return ind[l] < ind[r];
		});
		for (int i = 0; i < nl.size(); i++) {
			if (i > 0 && nl[i-1] == nl[i]) {
				offset[nl[i]]++;
				createdleaves++;
			} else {
				//printf("new node: %d\n", nl[i]);
				seen[nl[i]] = 1;
				newnodes.push_back(nl[i]);
				if (i == 0) {
					comps.push_back(nl[i]);
				} else {
					int l = lca(nl[i], nl[i-1]);
					if (!seen[l]) newnodes.push_back(l);
					seen[l] = 1;
					if (l != nl[i-1]) {
						while (comps.size() && depth[l] < depth[comps.back()]) {
							if (comps.size() > 1 && depth[comps[comps.size() - 2]] > depth[l]) {
								g2[comps[comps.size() - 2]].push_back(comps.back());
								//printf("Added compressed tree edge %d -> %d\n", comps[comps.size() - 2], comps.back());
							} else {
								g2[l].push_back(comps.back());
								//printf("Added compressed tree edge %d -> %d\n", l, comps.back());
							}
							comps.pop_back();
						}
						if (comps.empty() || comps.back() != l) comps.push_back(l);
					}
					comps.push_back(nl[i]);
				}
				if (!leaf[nl[i]]) createdleaves++;
				offset[nl[i]] = 1 - leaf[nl[i]];
			}
		}
		while (comps.size() > 1) {
			g2[comps[comps.size()-2]].push_back(comps.back());
			//printf("Added compressed tree edge %d -> %d\n", comps[comps.size() - 2], comps.back());
			comps.pop_back();
		}
		newans = rawtot + d;
		dfs3(comps[0], 1);
		if ((createdleaves + lvs[1]) & 1) {
			printf("-1\n");
		} else {
			printf("%d\n", newans);
		}
		// CLEANUP!!!
		newans = 0;
		for (int v : newnodes) {
			//printf("New nodes: %d\n", v);
			g2[v].clear();
			offset[v] = 0;
			seen[v] = 0;
		}
		newnodes.clear();
		comps.clear();
	}
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for (int i = 0; i < nl.size(); i++) {
      |                   ~~^~~~~~~~~~~
cleaning.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |  scanf("%d %d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   72 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |   scanf("%d", &d);
      |   ~~~~~^~~~~~~~~~
cleaning.cpp:90:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |    scanf("%d", &a);
      |    ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 78 ms 8920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 6524 KB Output is correct
2 Correct 24 ms 6524 KB Output is correct
3 Correct 63 ms 19696 KB Output is correct
4 Correct 116 ms 16876 KB Output is correct
5 Correct 164 ms 21228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 7164 KB Output is correct
2 Correct 25 ms 7164 KB Output is correct
3 Correct 86 ms 25720 KB Output is correct
4 Correct 113 ms 26864 KB Output is correct
5 Correct 78 ms 23672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 9336 KB Output is correct
2 Correct 41 ms 8320 KB Output is correct
3 Correct 17 ms 8064 KB Output is correct
4 Correct 17 ms 8448 KB Output is correct
5 Correct 18 ms 8576 KB Output is correct
6 Correct 57 ms 9056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 15352 KB Output is correct
2 Correct 259 ms 15736 KB Output is correct
3 Correct 119 ms 10748 KB Output is correct
4 Correct 232 ms 15608 KB Output is correct
5 Correct 256 ms 15740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 20916 KB Output is correct
2 Correct 147 ms 23032 KB Output is correct
3 Correct 152 ms 22648 KB Output is correct
4 Correct 147 ms 23288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 78 ms 8920 KB Output is correct
3 Correct 27 ms 6524 KB Output is correct
4 Correct 24 ms 6524 KB Output is correct
5 Correct 63 ms 19696 KB Output is correct
6 Correct 116 ms 16876 KB Output is correct
7 Correct 164 ms 21228 KB Output is correct
8 Correct 29 ms 7164 KB Output is correct
9 Correct 25 ms 7164 KB Output is correct
10 Correct 86 ms 25720 KB Output is correct
11 Correct 113 ms 26864 KB Output is correct
12 Correct 78 ms 23672 KB Output is correct
13 Correct 62 ms 9336 KB Output is correct
14 Correct 41 ms 8320 KB Output is correct
15 Correct 17 ms 8064 KB Output is correct
16 Correct 17 ms 8448 KB Output is correct
17 Correct 18 ms 8576 KB Output is correct
18 Correct 57 ms 9056 KB Output is correct
19 Correct 81 ms 15352 KB Output is correct
20 Correct 259 ms 15736 KB Output is correct
21 Correct 119 ms 10748 KB Output is correct
22 Correct 232 ms 15608 KB Output is correct
23 Correct 256 ms 15740 KB Output is correct
24 Correct 153 ms 20916 KB Output is correct
25 Correct 147 ms 23032 KB Output is correct
26 Correct 152 ms 22648 KB Output is correct
27 Correct 147 ms 23288 KB Output is correct
28 Correct 211 ms 14840 KB Output is correct
29 Correct 184 ms 22776 KB Output is correct
30 Correct 161 ms 21104 KB Output is correct
31 Correct 100 ms 26992 KB Output is correct
32 Correct 203 ms 15992 KB Output is correct
33 Correct 234 ms 20472 KB Output is correct
34 Correct 250 ms 23776 KB Output is correct
35 Correct 242 ms 23800 KB Output is correct