题解:CF681D Gifts by the List

· · 题解

大力出奇迹!

首先,对于每个 i,既然要求 a_i 在名单中位于 i 的其它祖先前面,我们可以考虑从 a_i 向 i 其它祖先都连一条边,最后在连成的图上跑拓扑排序即可。如果有环显然无解。

考虑如何实现树上一点向路径连边:使用重链剖分,这样路径就会被拆分成 \mathcal O(\log n) 个 DFS 序区间。

存下这些区间之后,我们不可能在拓扑排序中暴力遍历邻点。用区间最小值的懒标记线段树维护每个点的入度,这样就可以实现快速区间 -1,然后多次在线段树上二分入度为 0 的点加入队列。

时间复杂度 \mathcal O(n\log n)。

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int kMaxN = 1e5 + 5;

int tr[4 * kMaxN], tag[4 * kMaxN], a[kMaxN], dfn[kMaxN], top[kMaxN], siz[kMaxN], dep[kMaxN], son[kMaxN], fa[kMaxN], ind[kMaxN], id[kMaxN], pre[kMaxN], n, m, cnt, rt;
vector<int> e[kMaxN], node, ans;
vector<pair<int, int>> out[kMaxN];
queue<int> q;

void dfs1(int x, int f) {
  dep[x] = dep[f] + 1;
  fa[x] = f;
  siz[x] = 1;
  node.push_back(x);
  for (int i : e[x]) {
    dfs1(i, x);
    siz[x] += siz[i];
    if (siz[i] > siz[son[x]]) {
      son[x] = i;
    }
  }
}

void dfs2(int x, int t) {
  top[x] = t;
  id[dfn[x] = ++cnt] = x;
  if (!son[x]) {
    return;
  }
  dfs2(son[x], t);
  for (int i : e[x]) {
    if (i == son[x]) {
      continue;
    }
    dfs2(i, i);
  }
}

// x 为 y 祖先,求 x 的 y 那边儿子
int get(int x, int y) {
  for (; top[x] != top[y]; y = fa[top[y]]) {
    if (fa[top[y]] == x) {
      return top[y];
    }
  }
  return son[x];
}

// x 向树上路径 x-y 连边
void add(int p, int x, int y) {
  if (!x || !y) {
    return;
  }
  for (; top[x] != top[y]; x = fa[top[x]]) {
    if (dep[top[x]] < dep[top[y]]) {
      swap(x, y);
    }
    pre[dfn[top[x]]]++, pre[dfn[x] + 1]--;
    out[p].emplace_back(dfn[top[x]], dfn[x]);
  }
  if (dep[x] > dep[y]) {
    swap(x, y);
  }
  pre[dfn[x]]++, pre[dfn[y] + 1]--;
  out[p].emplace_back(dfn[x], dfn[y]);
}

void pushUp(int p) {
  tr[p] = min(tr[2 * p], tr[2 * p + 1]);
}

void modify(int p, int l, int r, int x, int val) {
  if (l == r) {
    tr[p] = val;
    return;
  }
  int m = (l + r) / 2;
  if (x <= m) {
    modify(2 * p, l, m, x, val);
  } else {
    modify(2 * p + 1, m + 1, r, x, val);
  }
  pushUp(p);
}

void addTag(int p, int d) {
  tag[p] += d;
  tr[p] += d;
}

void pushDown(int p) {
  if (tag[p]) {
    addTag(2 * p, tag[p]);
    addTag(2 * p + 1, tag[p]);
    tag[p] = 0;
  }
}

// 区间加
void modify(int p, int l, int r, int s, int t, int d) {
  if (s <= l && r <= t) {
    addTag(p, d);
    return;
  }
  pushDown(p);
  int m = (l + r) / 2;
  if (s <= m) {
    modify(2 * p, l, m, s, t, d);
  }
  if (m < t) {
    modify(2 * p + 1, m + 1, r, s, t, d);
  }
  pushUp(p);
}

// 二分出某区间中第一个入度为 0 的点,并标记为 1e9 表示不可再选
int query(int p, int l, int r, int s, int t) {
  if (l > t || r < s || tr[p] != 0) {
    return -1;
  }
  int m = (l + r) / 2;
  if (s <= l && r <= t) {
    if (l == r) {
      tr[p] = 1e9;
      return l;
    }
    pushDown(p);
    int res = -1;
    if (tr[2 * p] == 0) {
      res = query(2 * p, l, m, s, t);
    }
    if (res != -1) {
      pushUp(p);
      return res;
    }
    res = query(2 * p + 1, m + 1, r, s, t);
    pushUp(p);
    return res;
  }
  pushDown(p);
  int res = -1;
  if (s <= m) {
    res = query(2 * p, l, m, s, t);
  }
  if (res != -1) {
    pushUp(p);
    return res;
  }
  if (m < t) {
    res = query(2 * p + 1, m + 1, r, s, t);
  }
  pushUp(p);
  return res;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    e[u].push_back(v);
    ind[v]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (ind[i]) {
      continue;
    }
    int lst = cnt + 1;
    node.clear();
    rt = i;
    dfs1(i, 0);
    dfs2(i, i);
    for (int j : node) {
      if (a[j] == j) {
        add(j, rt, fa[j]);
      } else {
        int p = get(a[j], j);
        add(a[j], rt, fa[a[j]]);
        add(a[j], p, j);
      }
    }
    for (int j = lst + 1; j <= cnt; j++) {
      pre[j] += pre[j - 1];
    }
    for (int j = lst; j <= cnt; j++) {
      modify(1, 1, n, j, pre[j]);
    }
    int tot = 0;
    for (int j : node) {
      if (!pre[dfn[j]]) {
        q.push(j);
        modify(1, 1, n, dfn[j], 1e9);
      }
    }
    for (; q.size(); q.pop()) {
      tot++;
      int t = q.front();
      ans.push_back(t);
      for (auto [l, r] : out[t]) {
        modify(1, 1, n, l, r, -1);
      }
      while (1) {
        int p = query(1, 1, n, lst, cnt);
        if (p == -1) {
          break;
        }
        q.push(id[p]);
      }
    }
    if (tot != cnt - lst + 1) {
      cout << "-1\n";
      return 0;
    }
    pre[cnt + 1] = 0;
  }
  cout << ans.size() << '\n';
  for (int i : ans) {
    cout << i << '\n';
  }
  return 0;
}