답안 #1054831

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054831 2024-08-12T12:28:32 Z kiryl_krutsko Spring cleaning (CEOI20_cleaning) C++14
0 / 100
1000 ms 262144 KB

#include <iostream>
#include <vector>

using namespace std;

struct node {
	int num;
	vector<node*> conns;
};

int check_subtree(node* root, int parent_num) {
	int ans = 0;
	int odd_children_num = 0;
	for (auto child : root->conns) {
		if (child->num == parent_num) continue;
		int child_cost = check_subtree(child, root->num);
		if (child_cost >= 0) {
			ans += (child_cost + 1);
			odd_children_num++;
		}
		else ans += (2 - child_cost);
	}
	if (odd_children_num % 2 == 1) return ans;
	else return -ans;
}

int main()
{
	int n, q;
	cin >> n;
	vector<node> vec(n);
	int a, b; 
	vec[0].num = 0;
	for (int i = 1; i < n; i++) {
		vec[i].num = i;
		cin >> a >> b;
		a--; b--;
		vec[a].conns.push_back(&vec[b]);
		vec[b].conns.push_back(&vec[a]);
	}
	int total_cost = check_subtree(&vec[0], -1);
	if (total_cost < 0) cout << -total_cost;
	else if (vec[0].conns.size() == 1) cout << total_cost;
	else cout << -1;
	cout << endl;
	/*
	int new_num;
	for (int i = 0; i < q; i++) {
		cin >> new_num;
		for (int j = 0; j < new_num; j++) {
			cin >> a;
		}
		if ((n - 1) % 2 == 1 && n!= 2) {
			cout << -1 << endl;
		}
		else cout << n - 1 + new_num << endl;
	}*/
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:31:9: warning: unused variable 'q' [-Wunused-variable]
   31 |  int n, q;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 147 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 515 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 26 ms 5980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1041 ms 16720 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -