跳到正文

题解

CF1361E James and the Chase

Alex_Wei约 9 分钟
原文
文章信息与版本

文章信息

发布
2023年8月28日
收录
2026年6月2日
更新
2026年9月2日
篇幅
约 9 分钟
获赞
9
题目
CF1361E

版本

  1. v1当前

*CF1361E James and the Chase

很好的强连通题,需要对有向图 DFS 树有一定理解。

因为题目保证图强连通,所以从任意一个点出发 DFS 可达图上所有点。

判定一个点是否合法:以该点为根的任意 DFS 树只有树边和返祖边:横叉边和前向边将导致大于一条简单路径。

首先找到任意合法点:随机 100100 个点判定是否合法。如果合法点的数量不少于 0.2n0.2n,那么单组测试点正确率不低于 (1−0.8100)1000≈1−2×10−7(1 - 0.8 ^ {100}) ^ {1000}\approx 1 - 2\times 10 ^ {-7}。然后根据没有横插边和前向边的性质,求出其它每个点是否合法。

性质:点 xx 到它子树内的点有且仅有一条简单路径,因为一个点向下走的唯一途径是树边,向上走的唯一途径是返祖边。除了一定存在的一直向下走的简单路径,如果还有别的简单路径,则这条路径一定走了一条返祖边。这条返祖边一定指向 xx 的祖先,因为如果指向 xx 子树内的结点,那么由于走返祖边之前必须经过该结点,所以路径经过了重复点,不合法。而从 xx 的祖先回到目标点必须再次经过 xx,也不合法,所以这样的简单路径不存在。这说明,如果 xx 到 yy 有大于一条简单路径,那么 yy 不在 xx 的子树内,且这样的简单路径一定有且仅有一步从 xx 的子树内跳了出去。

称一条返祖边 u→vu\to v 覆盖 u,vu, v 的树上简单路径的除了 vv 以外的所有点,一个点可以通过覆盖它的返祖边离开它的子树。

如果点 xx 被大于一条边覆盖,那么它就有两种方法离开子树,然后向下走到它的父亲,即 xx 到 xx 的父亲的简单路径数量大于 11,xx 不合法。

如果 xx 只有一种方法离开子树,设对应返祖边为 u→vu\to v。因为不存在同时覆盖 xx 和 vv 的返祖边,也就是覆盖 vv 的返祖边的较深一端一定为 xx 的祖先,所以从 xx 经过 uu 走到 vv 之后,如果 vv 到其它点有大于一条简单路径,因为这样的路径一定会跳出 vv 的子树,所以一定不会经过 xx 以及它的子树,将 xx 到 vv 的路径接上这样的路径不会使得路径经过重复点。相反,如果 vv 到其它所有点有且仅有一条简单路径,那么容易通过反证法证明 xx 同样如此。

又因为一个非根节点必须能够离开子树(否则不满足原图强连通的性质),所以每个点被至少一条返祖边覆盖。因此,点 xx 合法的充要条件是:xx 为根,或者有且仅有一条返祖边 u→vu\to v 覆盖 xx 且 vv 合法。

用树上差分维护每个点被多少条边覆盖,再用树形 DP 求出每个点最浅能够跳到哪个祖先,最后从上往下 DP 求出每个点是否合法,时间复杂度 O(n+m)\mathcal{O}(n + m)。

总时间复杂度 O(c⋅(n+m))\mathcal{O}(c\cdot (n + m)),其中 cc 是随机次数。

C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ui = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdi = pair<double, int>;
using ull = unsigned long long;
using vint = vector<int>;
using vll = vector<ll>;

#define ppc(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)

bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnd(20060130);
int rd(int l, int r) {return rnd() % (r - l + 1) + l;}

constexpr int mod = 1e9 + 7;
void addt(int &x, int y) {x += y, x >= mod && (x -= mod);}
int add(int x, int y) {return x += y, x >= mod && (x -= mod), x;}
int ksm(int a, int b) {
  int s = 1;
  while(b) {
    if(b & 1) s = 1ll * s * a % mod;
    a = 1ll * a * a % mod, b >>= 1;
  }
  return s;
}

char buf[1 << 20], *p1 = buf, *p2 = buf;
#define getc() (p1 == p2 && (p2 = (p1 = buf) + \
  fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline int read() {
  int x = 0;
  char s = getc();
  while(!isdigit(s)) s = getc();
  while(isdigit(s)) x = x * 10 + s - '0', s = getc();
  return x;
}

#define putc(x) putchar(x)
inline void print(ui x) {
  if(x >= 10) print(x / 10);
  putc(x % 10 + '0');
}

// ---------- templates above ----------

constexpr int N = 1e5 + 5;

int n, m;
int dn, dfn[N], sz[N], fa[N];
vector<int> e[N];
void dfs1(int id) {
  dfn[id] = ++dn, sz[id] = 1;
  for(int it : e[id]) {
    if(dfn[it]) continue;
    dfs1(it), fa[it] = id, sz[id] += sz[it];
  }
}

int cov[N], g[N], f[N];
vector<int> ans;
void dfs2(int id) {
  g[id] = id;
  for(int it : e[id]) {
    if(fa[it] != id) {
      if(dfn[it] < dfn[g[id]]) g[id] = it;
      continue;
    }
    dfs2(it), cov[id] += cov[it];
    if(dfn[g[it]] < dfn[g[id]]) g[id] = g[it];
  }
}
void dfs3(int id) {
  f[id] = cov[id] >= 2 || f[g[id]];
  if(!f[id]) ans.push_back(id);
  for(int it : e[id]) {
    if(fa[it] == id) dfs3(it);
  } 
}

void mian() {
  cin >> n >> m;
  for(int i = 1; i <= n; i++) e[i].clear();
  for(int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    e[u].push_back(v);
  }
  for(int _ = 1; _ <= 100; _++) {
    int u = rd(1, n);
    for(int i = 1; i <= n; i++) {
      dfn[i] = cov[i] = f[i] = fa[i] = 0;
    }
    dn = 0, dfs1(u);
    int flg = 0;
    for(int i = 1; i <= n; i++) {
      for(int it : e[i]) {
        if(fa[it] == i) continue;
        if(dfn[i] < dfn[it] || dfn[i] >= dfn[it] + sz[it]) flg = 1;
        else cov[i]++, cov[it]--;
      }
    }
    if(flg) continue;
    dfs2(u), ans.clear(), dfs3(u);
    if(ans.size() * 5 >= n) {
      sort(ans.begin(), ans.end());
      for(int it : ans) cout << it << " ";
      cout << "\n";
      return;
    }
    else break;
  }
  cout << "-1\n";
}


bool Med;
int main() {
  fprintf(stderr, "%.3lf MB\n", (&Mbe - &Med) / 1048576.0);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  #ifdef ALEX_WEI
    FILE* IN = freopen("1.in", "r", stdin); 
    FILE* OUT = freopen("1.out", "w", stdout);
  #endif
  int T = 1;
  cin >> T;
  while(T--) mian();
  fprintf(stderr, "%d ms\n", int(1e3 * clock() / CLOCKS_PER_SEC));
  return 0;
}

/*
g++ a.cpp -o a -std=c++14 -O2 -DALEX_WEI
*/
原文
存档评论1 条抓取于