답안 #1055340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055340 2024-08-12T17:41:19 Z TAhmed33 Spring cleaning (CEOI20_cleaning) C++
34 / 100
1000 ms 20392 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
typedef long long ll;
const int MAXN = 2e5 + 25;
int n, q;
vector <int> adj[MAXN];
ll dp[MAXN];
int cc[MAXN];
void dfs (int pos, int par) {
	dp[pos] = 0; cc[pos] = 0;
	if (adj[pos].size() == 1 && par != -1) {
		dp[pos] = 1;
		cc[pos] = 1;
		return;
	}
	int cnt = 0;
	for (auto j : adj[pos]) {
		if (j != par) {
			dfs(j, pos);
			cc[pos] += cc[j];
			dp[pos] += dp[j];
		}
	}
	if (cc[pos] % 2 == 1) {
		if (par == -1) {
			dp[pos] = -1;
			return;
		}
		dp[pos] += 1;
		return;
	}
	if (par == -1) {
		return;
	}
	dp[pos] += 2;
	return;
}	
void solve () {
	cin >> n >> q;
	for (int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	while (q--) {
		int d; cin >> d;
		vector <int> c;
		for (int i = 1; i <= d; i++) {
			int x; cin >> x;
			c.push_back(x);
		}
		for (int i = 0; i < d; i++) {
			adj[c[i]].push_back(n + i + 1);
			adj[n + i + 1].push_back(c[i]);
		}
		int r = -1;
		for (int i = 1; i <= n + d; i++) {
			if (adj[i].size() >= 2) {
				r = i;
				break;
			}
		}
		dfs(r, -1);
		cout << dp[r] << '\n';
		for (int i = d - 1; i >= 0; i--) {
			adj[c[i]].pop_back();
			adj[n + i + 1].pop_back();
		}
	}
}		
signed main () {
	ios::sync_with_stdio(0); cin.tie(0);
	int tc = 1; //cin >> tc;
	while (tc--) solve();
}

Compilation message

cleaning.cpp: In function 'void dfs(int, int)':
cleaning.cpp:17:6: warning: unused variable 'cnt' [-Wunused-variable]
   17 |  int cnt = 0;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Execution timed out 1074 ms 8280 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10712 KB Output is correct
2 Correct 9 ms 10888 KB Output is correct
3 Correct 14 ms 11064 KB Output is correct
4 Correct 21 ms 13068 KB Output is correct
5 Correct 27 ms 14160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11228 KB Output is correct
2 Correct 10 ms 11228 KB Output is correct
3 Correct 23 ms 18424 KB Output is correct
4 Correct 32 ms 20392 KB Output is correct
5 Correct 24 ms 17372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 8760 KB Output is correct
2 Correct 88 ms 8024 KB Output is correct
3 Correct 126 ms 8028 KB Output is correct
4 Correct 176 ms 8536 KB Output is correct
5 Correct 144 ms 8748 KB Output is correct
6 Correct 185 ms 8560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1020 ms 9304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1057 ms 10648 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Execution timed out 1074 ms 8280 KB Time limit exceeded
3 Halted 0 ms 0 KB -