Submission #894256

#TimeUsernameProblemLanguageResultExecution timeMemory
894256boxThe Xana coup (BOI21_xanadu)C++17
100 / 100
69 ms20092 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ar array
#define sz(v) static_cast<int>(v.size())
#define all(v) (v).begin(), (v).end()
typedef pair<int, int> pi;
typedef long long ll;
typedef vector<int> vi;
 
const int N = 1e5, INF = 1e9;
 
int n, a[N];
ar<int, 2> dp[N][2];
vi g[N];
 
ar<int, 2> operator+(const ar<int, 2> &u, const ar<int, 2> &v) {
  return {min({INF, u[0] + v[0], u[1] + v[1]}),
          min({INF, u[0] + v[1], u[1] + v[0]})};
}
 
void dfs(int p, int i) {
  ar<int, 2> op0{0, INF}, op1{INF, 1};
  for (int j : g[i])
    if (p ^ j) {
      dfs(i, j);
      op0 = op0 + dp[j][0];
      op1 = op1 + dp[j][1];
    }
  if (a[i]) {
    swap(op0[0], op0[1]);
    swap(op1[0], op1[1]);
  }
  dp[i][0][0] = op0[0];
  dp[i][1][0] = op0[1];
  dp[i][0][1] = op1[0];
  dp[i][1][1] = op1[1];
}
 
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int h = 0; h < n - 1; h++) {
    int i, j;
    cin >> i >> j, i--, j--;
    g[i].push_back(j), g[j].push_back(i);
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  dfs(-1, 0);
  int x = min(dp[0][0][0], dp[0][0][1]);
  if (x == INF)
    cout << "impossible\n";
  else
    cout << x << '\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...