結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー warabi0906warabi0906
提出日時 2023-02-06 09:50:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 4,260 ms
コンパイル使用メモリ 230,252 KB
実行使用メモリ 34,344 KB
最終ジャッジ日時 2023-09-17 21:43:05
合計ジャッジ時間 11,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 163 ms
17,344 KB
testcase_12 AC 162 ms
17,340 KB
testcase_13 AC 160 ms
17,356 KB
testcase_14 AC 136 ms
17,496 KB
testcase_15 AC 182 ms
34,344 KB
testcase_16 AC 109 ms
12,252 KB
testcase_17 AC 109 ms
12,272 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 164 ms
17,376 KB
testcase_20 AC 170 ms
16,620 KB
testcase_21 AC 168 ms
16,740 KB
testcase_22 AC 152 ms
17,512 KB
testcase_23 AC 167 ms
16,696 KB
testcase_24 AC 165 ms
17,048 KB
testcase_25 AC 173 ms
16,200 KB
testcase_26 AC 185 ms
15,680 KB
testcase_27 AC 165 ms
17,572 KB
testcase_28 AC 152 ms
17,464 KB
testcase_29 AC 155 ms
17,724 KB
testcase_30 AC 188 ms
15,700 KB
testcase_31 AC 162 ms
17,248 KB
testcase_32 AC 170 ms
16,396 KB
testcase_33 AC 170 ms
16,476 KB
testcase_34 AC 165 ms
16,696 KB
testcase_35 AC 182 ms
15,972 KB
testcase_36 AC 181 ms
15,924 KB
testcase_37 AC 163 ms
16,852 KB
testcase_38 AC 185 ms
15,532 KB
testcase_39 AC 163 ms
17,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
	if ('A' <= c and c <= 'Z')return (int)(c - 'A');
	if ('a' <= c and c <= 'z')return (int)(c - 'a');
	if ('0' <= c and c <= '9')return (int)(c - '0');
	assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 62);
using minit = modint998244353;

template<typename T>
ostream& operator << (ostream& st, const vector<T> &v) {
	for (const T value : v) {
		st << value << " ";
	}
	return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
	for (T &value : v) {
		st >> value;
	}
	return st;
}

//

//

void solve() {
	//#1 入力
	int N;
	cin >> N;
	vector<vector<int>> G(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<int> color(N);
	cin >> color;

	//#2 再帰
	function<pair<int, int>(int, int)> dfs = [&](int now, int par) {
		//nowを根とする部分木を黒くするのに必要な操作回数
		// first...根は黒にできるか
		// second...最小の操作回数
		int res = 0, cnt = 0;
		for (const int next : G[now]) {
			if (next == par)continue;
			pair<int,int> p = dfs(next, now);
			cnt += 1 - p.first;
			res += p.second;
		}
		return make_pair((cnt ^ color[now]) & 1, res + cnt);
	};

	//#3 出力
	pair<int, int> p = dfs(0, -1);
	if (p.first)cout << p.second << endl;
	else cout << -1 << endl;
}
signed main() {
	int T = 1;
	// cin >> T;
	for (int i = 0; i < T; i++)
		solve();
	return 0;
}
0