QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#481337#2214. Link Cut Digraphnhuang685AC ✓276ms49224kbC++203.5kb2024-07-17 02:53:452024-07-17 02:53:46

Judging History

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

  • [2024-07-17 02:53:46]
  • 评测
  • 测评结果:AC
  • 用时:276ms
  • 内存:49224kb
  • [2024-07-17 02:53:45]
  • 提交

answer

/**
 * @author n685
 * @brief
 * @date 2024-07-16
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
void bar() {}
#endif

struct DSU {
  std::vector<int> val;
  int cnt{};
  int64_t sc{0};
  static int64_t contri(int sz) { return int64_t{sz} * int64_t{sz - 1} / 2; }
  DSU() = default;
  explicit DSU(int n) : val(n, -1), cnt(n) {}
  int find(int n) { return (val[n] < 0) ? n : (val[n] = find(val[n])); }
  bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) {
      return false;
    }
    if (val[u] > val[v]) {
      std::swap(u, v);
    }
    sc -= contri(-val[u]);
    sc -= contri(-val[v]);
    val[u] += val[v];
    val[v] = u;
    sc += contri(-val[u]);
    cnt--;
    return true;
  }
  bool connected(int u, int v) { return find(u) == find(v); }
  int size(int u) { return -val[find(u)]; }
  int count() const { return cnt; }
};
struct Edge {
  int u, v;
  int t;
};

int main() {
#ifndef LOCAL
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n, m;
  std::cin >> n >> m;
  std::vector<Edge> edges;
  edges.reserve(m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u;
    --v;
    edges.emplace_back(u, v, i);
  }

  std::vector<bool> vis(n);
  std::vector<std::vector<int>> adj(n), radj(n);
  std::vector<int> gr(n, -1);
  auto dfs1 = [&](auto &self, int node, std::vector<int> &t) -> void {
    for (int i : adj[node]) {
      if (!vis[i]) {
        vis[i] = true;
        self(self, i, t);
      }
    }
    t.push_back(node);
  };
  auto dfs2 = [&](auto &self, int node, int clr) -> void {
    for (int i : radj[node]) {
      if (gr[i] == -1) {
        gr[i] = clr;
        self(self, i, clr);
      }
    }
  };
  auto scc = [&](const std::vector<int> &nodes) -> void {
    for (int i : nodes) {
      vis[i] = false;
      gr[i] = -1;
    }
    std::vector<int> t;
    for (int i : nodes) {
      if (!vis[i]) {
        vis[i] = true;
        dfs1(dfs1, i, t);
      }
    }
    std::reverse(t.begin(), t.end());
    int cnt = 0;
    for (int i : t) {
      if (gr[i] == -1) {
        gr[i] = cnt;
        dfs2(dfs2, i, cnt);
        ++cnt;
      }
    }
    for (int i : nodes) {
      vis[i] = false;
    }
  };
  DSU dsu(n);
  auto dq = [&](auto &self, int l, int r, const std::vector<Edge> &ev) -> void {
    int mid = (l + r) / 2;
    std::vector<int> nodes;
    for (auto [u, v, t] : ev) {
      if (!vis[u]) {
        nodes.push_back(u);
        adj[u].clear();
        radj[u].clear();
        vis[u] = true;
      }
      if (!vis[v]) {
        nodes.push_back(v);
        adj[v].clear();
        radj[v].clear();
        vis[v] = true;
      }
      if (t <= mid) {
        adj[u].push_back(v);
        radj[v].push_back(u);
      }
    }
    scc(nodes);
    if (l == r) {
      for (auto [u, v, t] : ev) {
        if (gr[u] == gr[v]) {
          dsu.unite(edges[t].u, edges[t].v);
        }
      }
      std::cout << dsu.sc << '\n';
      return;
    }
    std::vector<Edge> el, er;
    for (auto [u, v, t] : ev) {
      if (gr[u] == gr[v]) {
        if (t <= mid) {
          el.emplace_back(u, v, t);
        }
      } else {
        er.emplace_back(gr[u], gr[v], t);
      }
    }
    self(self, l, mid, el);
    self(self, mid + 1, r, er);
  };
  dq(dq, 0, m - 1, edges);
}

Details

Test #1:

score: 100
Accepted
time: 271ms
memory: 45464kb

Test #2:

score: 0
Accepted
time: 264ms
memory: 44724kb

Test #3:

score: 0
Accepted
time: 259ms
memory: 46208kb

Test #4:

score: 0
Accepted
time: 258ms
memory: 49224kb

Test #5:

score: 0
Accepted
time: 263ms
memory: 45472kb

Test #6:

score: 0
Accepted
time: 275ms
memory: 44844kb

Test #7:

score: 0
Accepted
time: 270ms
memory: 45184kb

Test #8:

score: 0
Accepted
time: 263ms
memory: 45288kb

Test #9:

score: 0
Accepted
time: 200ms
memory: 46948kb

Test #10:

score: 0
Accepted
time: 269ms
memory: 46028kb

Test #11:

score: 0
Accepted
time: 265ms
memory: 45292kb

Test #12:

score: 0
Accepted
time: 263ms
memory: 45184kb

Test #13:

score: 0
Accepted
time: 213ms
memory: 47204kb

Test #14:

score: 0
Accepted
time: 228ms
memory: 47252kb

Test #15:

score: 0
Accepted
time: 83ms
memory: 21432kb

Test #16:

score: 0
Accepted
time: 240ms
memory: 35488kb

Test #17:

score: 0
Accepted
time: 230ms
memory: 34756kb

Test #18:

score: 0
Accepted
time: 223ms
memory: 34344kb

Test #19:

score: 0
Accepted
time: 276ms
memory: 44328kb

Test #20:

score: 0
Accepted
time: 252ms
memory: 47468kb

Test #21:

score: 0
Accepted
time: 254ms
memory: 46120kb

Test #22:

score: 0
Accepted
time: 251ms
memory: 46996kb

Test #23:

score: 0
Accepted
time: 259ms
memory: 46132kb

Test #24:

score: 0
Accepted
time: 251ms
memory: 46144kb

Test #25:

score: 0
Accepted
time: 252ms
memory: 45336kb

Test #26:

score: 0
Accepted
time: 244ms
memory: 45552kb

Test #27:

score: 0
Accepted
time: 256ms
memory: 44476kb