結果

問題 No.922 東北きりきざむたん
ユーザー 👑 emthrmemthrm
提出日時 2019-11-08 22:01:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 5,201 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 165,580 KB
実行使用メモリ 52,412 KB
最終ジャッジ日時 2023-10-13 03:51:28
合計ジャッジ時間 8,556 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 111 ms
25,504 KB
testcase_10 AC 41 ms
7,944 KB
testcase_11 AC 88 ms
18,324 KB
testcase_12 AC 105 ms
39,256 KB
testcase_13 AC 32 ms
11,976 KB
testcase_14 AC 184 ms
42,404 KB
testcase_15 AC 109 ms
43,656 KB
testcase_16 AC 234 ms
28,796 KB
testcase_17 AC 238 ms
31,964 KB
testcase_18 AC 239 ms
29,600 KB
testcase_19 AC 236 ms
29,260 KB
testcase_20 AC 239 ms
29,788 KB
testcase_21 AC 229 ms
26,188 KB
testcase_22 AC 230 ms
25,976 KB
testcase_23 AC 372 ms
38,816 KB
testcase_24 AC 381 ms
39,728 KB
testcase_25 AC 331 ms
41,280 KB
testcase_26 AC 342 ms
41,276 KB
testcase_27 AC 342 ms
41,280 KB
testcase_28 AC 179 ms
52,412 KB
testcase_29 AC 291 ms
42,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
struct UnionFind {
  UnionFind(int n) : data(n, -1) {}

  int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }

  void unite(int ver1, int ver2) {
    ver1 = root(ver1);
    ver2 = root(ver2);
    if (ver1 != ver2) {
      if (data[ver1] > data[ver2]) swap(ver1, ver2);
      data[ver1] += data[ver2];
      data[ver2] = ver1;
    }
  }

  bool same(int ver1, int ver2) { return root(ver1) == root(ver2); }

  int size(int ver) { return -data[root(ver)]; }

private:
  vector<int> data;
};

using CostType = int;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

struct LCA {
  vector<int> depth;
  vector<CostType> dist;

  LCA() {};
  LCA(const vector<vector<Edge> > &graph) : graph(graph) {
    n = graph.size();
    depth.resize(n);
    dist.resize(n);
    while ((1 << table_h) <= n) ++table_h;
    parent.resize(table_h, vector<int>(n));
  }

  void build(int root = 0) {
    dfs(-1, root, 0, 0);
    for (int i = 0; i + 1 < table_h; ++i) REP(ver, n) {
      parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
    }
  }

  int query(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    REP(i, table_h) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }

  CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; }

private:
  int n, table_h = 1;
  vector<vector<Edge> > graph;
  vector<vector<int> > parent;

  void dfs(int par, int ver, int now_depth, CostType now_dist) {
    depth[ver] = now_depth;
    dist[ver] = now_dist;
    parent[0][ver] = par;
    for (const Edge &e : graph[ver]) {
      if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost);
    }
  }
};

int main() {
  int n, m, q; cin >> n >> m >> q;
  vector<vector<int> > graph(n);
  UnionFind uf(n);
  while (m--) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
    uf.unite(u, v);
  }
  long long ans = 0;
  vector<int> sum(n, 0);
  map<int, map<int, int> > block;
  REP(i, n) {
    int rt = uf.root(i), sz = block[rt].size();
    block[rt][i] = sz;
  }
  map<int, LCA> lca;
  for (const auto &pr : block) {
    auto mp = pr.second;
    vector<vector<Edge> > g(mp.size());
    for (auto &pr : mp) {
      for (int e : graph[pr.first]) {
        g[mp[pr.first]].emplace_back(mp[pr.first], mp[e], 1);
      }
    }
    lca[pr.first] = LCA(g);
    lca[pr.first].build();
  }
  while (q--) {
    int a, b; cin >> a >> b; --a; --b;
    if (uf.same(a, b)) {
      int rt = uf.root(a);
      ans += lca[rt].distance(block[rt][a], block[rt][b]);
    } else {
      ++sum[a];
      ++sum[b];
    }
  }
  vector<bool> visited(n);
  vector<long long> dp(n, 0);
  function<void(int, int)> dfs1 = [&](int par, int ver) {
    for (int e : graph[ver]) {
      if (e != par) {
        visited[e] = true;
        dfs1(ver, e);
        sum[ver] += sum[e];
        dp[ver] += dp[e] + sum[e];
      }
    }
  };
  int root;
  long long tmp;
  function<void(int, int, long long)> dfs2 = [&](int par, int ver, long long par_dp) {
    tmp = min(tmp, par_dp + dp[ver]);
    for (int e : graph[ver]) if (e != par) {
      long long nx_dp = par_dp + dp[ver] - (dp[e] + sum[e]);
      nx_dp += root - sum[e];
      dfs2(ver, e, nx_dp);
    }
  };
  REP(i, n) {
    if (!visited[i]) {
      dfs1(-1, i);
      root = sum[i];
      tmp = dp[i];
      dfs2(-1, i, 0);
      ans += tmp;
    }
  }
  cout << ans << '\n';
  return 0;
}
0