제출 #736418

#제출 시각아이디문제언어결과실행 시간메모리
736418hollwo_pelwThe Xana coup (BOI21_xanadu)C++17
100 / 100
68 ms18432 KiB
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/trie_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_cxx; // using namespace __gnu_pbds; void Hollwo_Pelw(); signed main(){ #ifndef hollwo_pelw_local if (fopen(".inp", "r")) assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout)); #else using namespace chrono; auto start = steady_clock::now(); #endif cin.tie(0), cout.tie(0) -> sync_with_stdio(0); int testcases = 1; // cin >> testcases; for (int test = 1; test <= testcases; test++){ // cout << "Case #" << test << ": "; Hollwo_Pelw(); } #ifdef hollwo_pelw_local auto end = steady_clock::now(); cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl; #endif } const int N = 1e5 + 5, inf = 1e9; int n, a[N], dp[N][2][2]; vector<int> adj[N]; void dfs(int u, int p) { for (int i = 0; i < 2; i++) dp[u][i][a[u] ^ i] = i; for (int v : adj[u]) if (v != p) { dfs(v, u); for (int i = 0; i < 2; i++) { int dpu0 = dp[u][i][0], dpu1 = dp[u][i][1]; dp[u][i][0] = min({dpu0 + dp[v][0][i], dpu1 + dp[v][1][i], inf}); dp[u][i][1] = min({dpu0 + dp[v][1][i], dpu1 + dp[v][0][i], inf}); } } } void Hollwo_Pelw(){ memset(dp, 0x3f, sizeof dp); cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) cin >> a[i]; dfs(1, 0); int res = min(dp[1][0][0], dp[1][1][0]); if (res > n) cout << "impossible\n"; else cout << res << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...