結果

問題 No.1194 Replace
ユーザー 👑 emthrmemthrm
提出日時 2020-08-22 15:06:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 648 ms / 2,000 ms
コード長 4,194 bytes
コンパイル時間 3,210 ms
コンパイル使用メモリ 234,900 KB
実行使用メモリ 120,720 KB
最終ジャッジ日時 2023-08-05 12:19:08
合計ジャッジ時間 14,391 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 462 ms
75,244 KB
testcase_01 AC 493 ms
79,296 KB
testcase_02 AC 390 ms
66,200 KB
testcase_03 AC 310 ms
57,272 KB
testcase_04 AC 514 ms
79,400 KB
testcase_05 AC 482 ms
74,580 KB
testcase_06 AC 450 ms
70,768 KB
testcase_07 AC 634 ms
120,168 KB
testcase_08 AC 633 ms
120,620 KB
testcase_09 AC 648 ms
119,480 KB
testcase_10 AC 647 ms
120,560 KB
testcase_11 AC 634 ms
120,720 KB
testcase_12 AC 601 ms
119,444 KB
testcase_13 AC 393 ms
42,204 KB
testcase_14 AC 320 ms
33,960 KB
testcase_15 AC 296 ms
41,320 KB
testcase_16 AC 389 ms
42,980 KB
testcase_17 AC 262 ms
37,176 KB
testcase_18 AC 249 ms
31,180 KB
testcase_19 AC 387 ms
46,680 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 108 ms
20,688 KB
testcase_24 AC 34 ms
10,740 KB
testcase_25 AC 13 ms
4,884 KB
testcase_26 AC 118 ms
15,072 KB
testcase_27 AC 19 ms
5,360 KB
testcase_28 AC 53 ms
9,092 KB
testcase_29 AC 7 ms
4,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
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()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = bool;
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 &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

struct SCC {
  vector<int> id;
  vector<vector<int>> vertices;
  vector<vector<Edge>> comp;

  SCC(const vector<vector<Edge>> &graph, bool heavy = false) : graph(graph), heavy(heavy) {
    n = graph.size();
    rev_graph.resize(n);
    REP(i, n) for (const Edge &e : graph[i]) {
      rev_graph[e.dst].emplace_back(e.dst, e.src, e.cost);
    }
    used.assign(n, false);
    id.assign(n, -1);
    REP(i, n) {
      if (!used[i]) dfs(i);
    }
    int now = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if (heavy) vertices.emplace_back();
        rev_dfs(order[i], now++);
      }
    }
    comp.resize(now);
    REP(i, n) for (const Edge &e : graph[i]) {
      if (id[i] != id[e.dst]) comp[id[i]].emplace_back(id[i], id[e.dst], e.cost);
    }
    // if (heavy) {
    //   REP(i, now) sort(ALL(vertices[i]));
    // }
  }

private:
  bool heavy;
  int n;
  vector<vector<Edge>> graph, rev_graph;
  vector<bool> used;
  vector<int> order;

  void dfs(int ver) {
    used[ver] = true;
    for (const Edge &e : graph[ver]) {
      if (!used[e.dst]) dfs(e.dst);
    }
    order.emplace_back(ver);
  }

  void rev_dfs(int ver, int now) {
    id[ver] = now;
    if (heavy) vertices[now].emplace_back(ver);
    for (Edge &e : rev_graph[ver]) {
      if (id[e.dst] == -1) rev_dfs(e.dst, now);
    }
  }
};

vector<int> topological_sort(const vector<vector<Edge>> &graph) {
  int n = graph.size();
  vector<int> deg(n, 0);
  REP(i, n) {
    for (const Edge &e : graph[i]) ++deg[e.dst];
  }
  queue<int> que;
  REP(i, n) {
    if (deg[i] == 0) que.emplace(i);
  }
  vector<int> res;
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    res.emplace_back(ver);
    for (const Edge &e : graph[ver]) {
      if (--deg[e.dst] == 0) que.emplace(e.dst);
    }
  }
  return res.size() == n ? res : vector<int>();
}

int main() {
  int n, m; cin >> n >> m;
  vector<vector<Edge>> graph;
  map<int, int> mp;
  vector<int> rev;
  while (m--) {
    int b, c; cin >> b >> c;
    if (mp.count(b) == 0) {
      graph.emplace_back();
      mp[b] = rev.size();
      rev.emplace_back(b);
    }
    if (mp.count(c) == 0) {
      graph.emplace_back();
      mp[c] = rev.size();
      rev.emplace_back(c);
    }
    graph[mp[b]].emplace_back(mp[b], mp[c]);
  }
  SCC scc(graph, true);
  int l = scc.comp.size();
  vector<int> val(l, 0);
  REP(i, l) {
    for (int e : scc.vertices[i]) chmax(val[i], rev[e]);
  }
  // REP(i, l) cout << val[i] << " \n"[i + 1 == l];
  vector<int> t = topological_sort(scc.comp);
  reverse(ALL(t));
  for (int v : t) for (const Edge &e : scc.comp[v]) chmax(val[v], val[e.dst]);
  // REP(i, l) cout << val[i] << " \n"[i + 1 == l];
  ll ans = 1LL * n * (n + 1) / 2;
  REP(i, l) for (int e : scc.vertices[i]) {
    ans -= rev[e];
    ans += max(rev[e], val[i]);
  }
  cout << ans << '\n';
  return 0;
}
0