QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344521#837. Giant PenguinJWRuixiRE 0ms0kbC++173.1kb2024-03-04 18:24:222024-03-04 18:24:23

Judging History

你现在查看的是最新测评结果

  • [2024-03-04 18:24:23]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-03-04 18:24:22]
  • 提交

answer

#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;

using vi = vector<int>;

constexpr int N = 1e5 + 9;
constexpr int M = 2e5 + 9;
constexpr int K = 12;
constexpr int INF = 1e9;
int n, m, k, Q, as[M];
vi G[N];

struct qNode {
  int o, x, y;
  qNode (int o = 0, int x = 0, int y = 0) : o(o), x(x), y(y) {}
};
vector<qNode> q[N], qq;

bool h[N];
vi e[N], g[N];
IL void dfs (int u, int fa) {
  h[u] = 1;
  for (int v : G[u]) {
    if (v ^ fa) {
      if (h[v]) {
        e[u].eb(v);
      } else {
        g[u].eb(v);
        dfs(v, u);
      }
    }
  }
}

bool used[N], vis[N];
vi vc, key;

IL void dfs1 (int u) {
  vc.eb(u);
  if (!q[u].empty()) {
    int sz = qq.size();
    qq.insert(qq.end(), q[u].begin(), q[u].end());
    inplace_merge(qq.begin(), qq.begin() + sz, qq.end(), [] (auto i, auto j) {
      return i.y < j.y;
    });
  }
  bool fl = 0;
  for (int v : g[u]) {
    if (used[v]) {
      continue;
    }
    if (vis[v]) {
      fl = 1;
    } else {
      dfs1(v);
    }
  }
  if (fl) {
    key.eb(u);
  }
}

IL void dfs2 (int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    if (!used[v]) {
      dfs2(v);
    }
  }
}

int R, sz[N], S;

IL void dfs3 (int u) {
  sz[u] = 1;
  int mx = 0;
  for (int v : g[u]) {
    if (used[v]) {
      continue;
    }
    dfs3(v);
    sz[u] += sz[v];
    mx = max(mx, sz[v]);
  }
  mx = max(mx, S - sz[u]);
  if (mx <= S / 2) {
    R = u;
  }
}

IL void bfs (int s, int *d) {
  for (auto v : vc) {
    d[v] = INF;
  }
  queue<int> q;
  d[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (vis[v] && d[v] > d[u] + 1) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
}

int d[K][N], mn[K];

IL void dfz (int u) {
  vc = key = {u};
  qq = q[u];
  for (int v : G[u]) {
    if (!used[v]) {
      dfs1(v);
      dfs2(v);
    }
  }
  vis[u] = 1;
  int kk = key.size();
  L (i, 0, kk - 1) {
    bfs(key[i], d[i]);
    mn[i] = INF;
  }
  L (i, 1, (int)qq.size() - 1) {
    assert(qq[i].y > qq[i - 1].y);
  }
  for (auto [o, x, y] : qq) {
    if (o & 1) {
      L (i, 0, kk - 1) {
        mn[i] = min(mn[i], d[i][x]);
      }
    } else {
      L (i, 0, kk - 1) {
        as[y] = min(as[y], mn[i] + d[i][x]);
      }
    }
  }
  for (int v : vc) {
    vis[v] = 0;
  }
  used[u] = 1;
  dfs3(u);
  for (int v : G[u]) {
    if (!used[v]) {
      S = sz[v];
      dfs3(v);
      dfz(R);
    }
  }
}

int main () {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> k;
  L (i, 1, m) {
    int u, v;
    cin >> u >> v;
    G[u].eb(v);
    G[v].eb(u);
  }
  cin >> Q;
  fill(as + 1, as + Q + 1, -1);
  L (i, 1, Q) {
    int o, x;
    cin >> o >> x;
    q[x].eb(o, x, i);
    if (o ^ 1) {
      as[i] = INF;
    }
  }
  dfs(1, 0);
  S = n;
  dfs3(1);
  dfz(R);
  L (i, 1, Q) {
    if (~as[i]) {
      cout << as[i] << '\n';
    }
  }
}
// I love WHQ!

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

5 4 0
1 2
2 3
3 4
4 5
7
1 1
1 5
2 1
2 2
2 3
2 4
2 5

output:


result: