QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#555975#2214. Link Cut DigraphetohariAC ✓346ms37984kbC++203.0kb2024-09-10 13:18:572024-09-10 13:18:58

Judging History

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

  • [2024-09-10 13:18:58]
  • 评测
  • 测评结果:AC
  • 用时:346ms
  • 内存:37984kb
  • [2024-09-10 13:18:57]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

namespace std {
#ifndef LOCAL
#define cerr \
  if (0) cerr
#endif
}  // namespace std

struct DSU {
  vector<int> par;
  int64_t cur_ans;

  DSU(int n = 0) {
    par.assign(n + 5, -1);
    cur_ans = 0;
  }

  int root(int u) {
    if (par[u] < 0)
      return u;
    return par[u] = root(par[u]);
  }

  int size(int u) {
    return -par[root(u)];
  }

  bool join(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v)
      return false;
    cur_ans += 1ll * -par[u] * -par[v];
    if (par[u] > par[v])
      swap(u, v);
    par[u] += par[v];
    par[v] = u;
    return true;
  }
};

const int N = 2.5e5 + 5;

int n, m;
pair<int, int> ed[N];

DSU dsu(N);
int64_t ans[N];
vector<int> nodes;
vector<int> g[N];
int low[N], num[N], time_dfs, del[N];
int id_scc[N];
vector<int> stk;
vector<vector<int>> scc;
vector<int> T[N];

void dfs(int u) {
  low[u] = num[u] = ++time_dfs;
  stk.push_back(u);
  for (int v : g[u]) {
    if (del[v]) {
      continue;
    }
    if (!num[v]) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else {
      low[u] = min(low[u], num[v]);
    }
  }
  if (low[u] == num[u]) {
    scc.push_back({});
    int v;
    do {
      v = stk.back();
      scc.back().push_back(v);
      id_scc[v] = scc.size();
      del[v] = 1;
      stk.pop_back();
    } while (v != u);
  }
}

void add_edge(int u, int v) {
  nodes.push_back(u);
  nodes.push_back(v);
  g[u].push_back(v);
}

void reset() {
  for (int u : nodes) {
    low[u] = num[u] = del[u] = 0;
    id_scc[u] = 0;
    g[u].clear();
  }
  nodes.clear();
  stk.clear();
  scc.clear();
  time_dfs = 0;
}

void solve(int l, int r, vector<int> edges) {
  if (l > r) {
    return;
  }
  int mid = (l + r) >> 1;
  reset();
  for (int i : edges) {
    if (i <= mid) {
      auto [x, y] = ed[i];
      x = dsu.root(x);
      y = dsu.root(y);
      add_edge(x, y);
    }
  }
  for (int u : nodes) {
    if (!num[u]) {
      dfs(u);
    }
  }
  vector<int> g1, g2;
  for (int i : edges) {
    if (i <= mid) {
      auto [u, v] = ed[i];
      u = dsu.root(u), v = dsu.root(v);
      if (id_scc[u] == id_scc[v]) {
        g1.push_back(i);
      } else {
        g2.push_back(i);
      }
    } else {
      g2.push_back(i);
    }
  }
  solve(l, mid - 1, g1);
  for (int i : g1) {
    dsu.join(ed[i].first, ed[i].second);
  }
  ans[mid] = dsu.cur_ans;
  solve(mid + 1, r, g2);
  edges.clear();
  g1.clear();
  g2.clear();
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
#define task "a"
#else
#define task ""
#endif
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    auto& [u, v] = ed[i];
    cin >> u >> v;
  }
  vector<int> id(m);
  iota(id.begin(), id.end(), 1);
  solve(1, m, id);
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 332ms
memory: 35580kb

Test #2:

score: 0
Accepted
time: 332ms
memory: 34456kb

Test #3:

score: 0
Accepted
time: 338ms
memory: 35368kb

Test #4:

score: 0
Accepted
time: 334ms
memory: 36376kb

Test #5:

score: 0
Accepted
time: 346ms
memory: 35644kb

Test #6:

score: 0
Accepted
time: 329ms
memory: 35824kb

Test #7:

score: 0
Accepted
time: 337ms
memory: 34776kb

Test #8:

score: 0
Accepted
time: 336ms
memory: 34860kb

Test #9:

score: 0
Accepted
time: 295ms
memory: 37732kb

Test #10:

score: 0
Accepted
time: 339ms
memory: 35864kb

Test #11:

score: 0
Accepted
time: 332ms
memory: 35600kb

Test #12:

score: 0
Accepted
time: 336ms
memory: 35100kb

Test #13:

score: 0
Accepted
time: 311ms
memory: 37984kb

Test #14:

score: 0
Accepted
time: 314ms
memory: 37660kb

Test #15:

score: 0
Accepted
time: 158ms
memory: 24188kb

Test #16:

score: 0
Accepted
time: 273ms
memory: 28684kb

Test #17:

score: 0
Accepted
time: 286ms
memory: 28496kb

Test #18:

score: 0
Accepted
time: 285ms
memory: 28604kb

Test #19:

score: 0
Accepted
time: 344ms
memory: 35020kb

Test #20:

score: 0
Accepted
time: 333ms
memory: 35120kb

Test #21:

score: 0
Accepted
time: 328ms
memory: 35076kb

Test #22:

score: 0
Accepted
time: 334ms
memory: 35180kb

Test #23:

score: 0
Accepted
time: 339ms
memory: 35172kb

Test #24:

score: 0
Accepted
time: 335ms
memory: 34976kb

Test #25:

score: 0
Accepted
time: 322ms
memory: 34900kb

Test #26:

score: 0
Accepted
time: 332ms
memory: 34620kb

Test #27:

score: 0
Accepted
time: 328ms
memory: 34100kb