Submission #537298

#TimeUsernameProblemLanguageResultExecution timeMemory
537298skittles1412The Xana coup (BOI21_xanadu)C++17
100 / 100
131 ms22792 KiB
#include "bits/extc++.h" using namespace std; template <typename T> void dbgh(const T& t) { cerr << t << endl; } template <typename T, typename... U> void dbgh(const T& t, const U&... u) { cerr << t << " | "; dbgh(u...); } #ifdef DEBUG #define dbg(...) \ cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \ << ": "; \ dbgh(__VA_ARGS__) #else #define cerr \ if (false) \ cerr #define dbg(...) #endif #define endl "\n" #define long int64_t #define sz(x) int((x).size()) const int maxn = 1e5 + 5; bool arr[maxn]; int memo[maxn][2][2]; vector<int> graph[maxn]; void pdfs(int u) { for (auto& v : graph[u]) { graph[v].erase(find(begin(graph[v]), end(graph[v]), u)); pdfs(v); } } int dp(int u, bool p, bool c) { int &ans = memo[u][p][c]; if (ans != -1) { return ans; } int cdp[2]; cdp[0] = cdp[1] = 1e9; cdp[p ^ c] = 0; for (auto& v : graph[u]) { int ndp[2]; for (int i = 0; i < 2; i++) { ndp[i] = min({int(1e9), cdp[i] + dp(v, c, false), cdp[i ^ 1] + 1 + dp(v, c, true)}); } cdp[0] = ndp[0]; cdp[1] = ndp[1]; } return ans = cdp[arr[u]]; } void solve() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; graph[u].push_back(v); graph[v].push_back(u); } for (int i = 0; i < n; i++) { cin >> arr[i]; } pdfs(0); memset(memo, -1, sizeof(memo)); int ans = min(dp(0, false, false), 1 + dp(0, false, true)); if (ans < int(1e9)) { cout << ans << endl; } else { cout << "impossible" << endl; } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cin.exceptions(ios::failbit); solve(); }
#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...