Submission #1298652

#TimeUsernameProblemLanguageResultExecution timeMemory
1298652lmquanSynchronization (JOI13_synchronization)C++20
100 / 100
519 ms22300 KiB
#define taskname ""
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 100005;
const int kMaxLog = 18;

int n, m, q, timer;
int in[kMaxN], out[kMaxN], jump[kMaxLog][kMaxN];
int info_count[kMaxN], duplicate_count[kMaxN];
bool enabled[kMaxN];
vector<int> adj[kMaxN];
vector<pair<int, int>> edges;

void DFS(int u, int p) {
  in[u] = ++timer;
  for (int v : adj[u]) {
    if (v == p) {
      continue;
    }
    jump[0][v] = u;
    for (int i = 1; i < kMaxLog; i++) {
      jump[i][v] = jump[i - 1][jump[i - 1][v]];
    }
    DFS(v, u);
  }
  out[u] = timer;
}

struct FenwickTree {
  int n;
  vector<int> bit;

  FenwickTree() {}
  FenwickTree(int _n) : n(_n) {
    bit.resize(n + 1, 0);
  }

  void Update(int pos, int val) {
    for (int i = pos; i <= n; i += i & (-i)) {
      bit[i] += val;
    }
  }

  int PrefixSum(int pos) {
    int sum = 0;
    for (int i = pos; i > 0; i -= i & (-i)) {
      sum += bit[i];
    }
    return sum;
  }
} ft;

int FindSet(int u) {
  for (int i = kMaxLog - 1; i >= 0; i--) {
    if (jump[i][u] >= 1 && ft.PrefixSum(in[jump[i][u]]) == ft.PrefixSum(in[u])) {
      u = jump[i][u];
    }
  }
  return u;
}

int main() {
  if (fopen(taskname".inp", "r")) {
    freopen(taskname".inp", "r", stdin);
    freopen(taskname".out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    edges.push_back({x, y});
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  int root = 1;
  DFS(root, -1);

  ft = FenwickTree(n);
  for (int i = 1; i <= n; i++) {
    info_count[i] = 1;
    if (i != root) {
      ft.Update(in[i], 1);
      ft.Update(out[i] + 1, -1);
    }
  }

  while (m--) {
    int d;
    cin >> d;
    d--;
    int u = edges[d].first, v = edges[d].second;
    if (jump[0][u] == v) {
      swap(u, v);
    }
    if (!enabled[d]) {
      info_count[FindSet(u)] += info_count[v] - duplicate_count[v];
      ft.Update(in[v], -1);
      ft.Update(out[v] + 1, 1);
    } else {
      info_count[v] = info_count[FindSet(u)];
      duplicate_count[v] = info_count[v];
      ft.Update(in[v], 1);
      ft.Update(out[v] + 1, -1);
    }

    enabled[d] = !enabled[d];
  }

  while (q--) {
    int c;
    cin >> c;
    cout << info_count[FindSet(c)] << '\n';
  }

  return 0;
}

Compilation message (stderr)

synchronization.cpp: In function 'int main()':
synchronization.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen(taskname".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen(taskname".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...