/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 2ms 320.0 KiB
#3 Accepted 2ms 324.0 KiB
#4 Accepted 2ms 324.0 KiB
#5 Accepted 32ms 1.355 MiB
#6 Accepted 32ms 1.289 MiB
#7 Accepted 415ms 8.879 MiB
#8 Accepted 390ms 8.68 MiB
#9 Accepted 387ms 8.879 MiB
#10 Accepted 210ms 6.953 MiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n, q;
  cin >> n >> q;
  
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  
  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);
  }
  
  int timer = 0;
  vector<int> in(n, 0), out(n, 0);

  auto dfs = [&](auto &&self, int node, int par) -> void {
    in[node] = ++timer;
    for (auto &nodes : g[node]) {
      if (nodes != par) {
        self(self, nodes, node);
      }
    }
    out[node] = timer;
  };

  dfs(dfs, 0, -1);

  vector<int> bit(2 * n + 1, 0);
  auto update = [&](int idx, int val) -> void {
    for (; idx < 2 * n; idx += idx & -idx) bit[idx] ^= val;
  };
  auto query = [&](int idx) -> int {
    int S = 0;
    for (;idx; idx -= idx & -idx) S ^= bit[idx];
    return S;
  };

  while (q--) {
    int v;
    cin >> v, --v;
    update(in[v], 1);
    update(out[v] + 1, 1);
  }
  for (int i = 0; i < n; i++) {
    a[i] ^= (query(in[i]) % 2);
  }
  cout << "Case " << cs << ": ";
  for (int i = 0; i < n; i++) {
    cout << a[i] << " \n"[i == n - 1];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1003 Tahsin and Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-10 18:10:45
Judged At
2024-11-11 03:28:03
Judged By
Score
100
Total Time
415ms
Peak Memory
8.879 MiB