Submission #1322304

#TimeUsernameProblemLanguageResultExecution timeMemory
1322304behradSynchronization (JOI13_synchronization)C++20
Compilation error
0 ms0 KiB
#ifndef ONLINE_JUDGE
#include "/home/bgopc/template/pch.hpp"
#endif
#include <bits/stdc++.h>
using namespace std;
// * No One Dies a Virgin, Life Fucks Us All
typedef long long ll;
#define nl '\n'
#define ff first
#define ss second
#define pb push_back
#define sik(x) {cout << x << nl; return;}
constexpr ll maxn = 2e5+10, mod = 1e9 + 7, inf = 1e17, SQ = 450, LG = 20;
typedef pair<int, int> pii;

int n, m, q, in[maxn], out[maxn], T = 1, big[maxn], sz[maxn], hd[maxn], h[maxn], par[maxn], ans[maxn], lst[maxn];
vector<int> g[maxn];
auto cmp = [] (int u, int v) { return in[u] < in[v]; };
set<int, decltype(cmp)> have[maxn];
// set<pii> have[maxn];
pii E[maxn];

void PreDfs(int v) {
  big[v] = -1;
  sz[v] = 1;
  for (int &u : g[v]) {
    g[u].erase(find(g[u].begin(), g[u].end(), v));
  
    par[u] = v;
    h[u] = h[v] + 1;
    PreDfs(u);
    
    sz[v] += sz[u];
    if (big[v] == -1 || sz[u] > sz[big[v]]) {
      big[v] = u;
      swap(g[v][0], u);
    }
  }
}

void dfs(int v) {
  in[v] = T ++;
  for (int u : g[v]) {
    hd[u] = u == big[v] ? hd[v] : u;
    dfs(u);
  }
  out[v] = T;
}

inline bool upd(int e) {
  auto [u, v] = E[e];

  u = hd[u];
  auto [it, f] = have[u].insert(v);
  if (!f) have[u].erase(it);

  return !f;
}

inline int root(int v) {
  int u = v;
  while (1) {
    auto &s = have[hd[v]];
    if (!s.empty()) {
      auto it = s.upper_bound(u);
      if (it != s.begin()) { --it; return *it; }
    }
    if (hd[v] == 1) return 1;
    v = par[hd[v]];
  }
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0); 
  cin >> n >> m >> q;
  for (int i = 1 , u ,v ; i <= n - 1 ; i ++) {
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
    E[i] = {u, v};
  }

  PreDfs(1);
  hd[1] = 1;
  dfs(1);

  for (int i = 1 ; i <= n ; i ++) ans[i] = 1;

  for (int i = 1 ; i <= n - 1 ; i ++) {
    if (h[E[i].ff] > h[E[i].ss]) swap(E[i].ff, E[i].ss);
    upd(i);
  }

  for (int e ; m -- ; ) {
    cin >> e;
    auto [u, v] = E[e];
    if (upd(e)) ans[root(u)] += ans[v] - lst[v];
    else ans[v] = lst[v] = ans[root(u)];
  }

  for (int v ; q -- ; ) {
    cin >> v;
    cout << ans[root(v)] << nl;
  }
  return 0;
}

Compilation message (stderr)

synchronization.cpp:2:10: fatal error: /home/bgopc/template/pch.hpp: No such file or directory
    2 | #include "/home/bgopc/template/pch.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.