結果

問題 No.1194 Replace
ユーザー 👑 emthrmemthrm
提出日時 2020-08-22 15:06:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 4,194 bytes
コンパイル時間 3,608 ms
コンパイル使用メモリ 228,728 KB
実行使用メモリ 119,148 KB
最終ジャッジ日時 2024-04-23 09:29:06
合計ジャッジ時間 13,053 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 438 ms
75,568 KB
testcase_01 AC 455 ms
79,656 KB
testcase_02 AC 350 ms
66,236 KB
testcase_03 AC 285 ms
57,404 KB
testcase_04 AC 454 ms
79,624 KB
testcase_05 AC 420 ms
74,648 KB
testcase_06 AC 392 ms
70,464 KB
testcase_07 AC 564 ms
118,616 KB
testcase_08 AC 563 ms
118,760 KB
testcase_09 AC 570 ms
118,756 KB
testcase_10 AC 558 ms
118,760 KB
testcase_11 AC 566 ms
119,148 KB
testcase_12 AC 555 ms
118,636 KB
testcase_13 AC 357 ms
42,548 KB
testcase_14 AC 290 ms
34,004 KB
testcase_15 AC 282 ms
41,476 KB
testcase_16 AC 350 ms
43,084 KB
testcase_17 AC 243 ms
37,460 KB
testcase_18 AC 225 ms
31,356 KB
testcase_19 AC 368 ms
45,496 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 97 ms
20,796 KB
testcase_24 AC 30 ms
10,936 KB
testcase_25 AC 12 ms
5,376 KB
testcase_26 AC 108 ms
15,368 KB
testcase_27 AC 18 ms
5,376 KB
testcase_28 AC 47 ms
9,272 KB
testcase_29 AC 6 ms
5,376 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