結果

問題 No.1194 Replace
ユーザー MayimgMayimg
提出日時 2020-08-25 02:20:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,928 bytes
コンパイル時間 2,798 ms
コンパイル使用メモリ 220,484 KB
実行使用メモリ 103,388 KB
最終ジャッジ日時 2024-04-24 04:05:52
合計ジャッジ時間 15,528 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

map<int, int> zip;
vector<int> unzip;
template <typename T> void compress(vector<T>& v) {
  for (int i = 0; i < (int) v.size(); i++) unzip.push_back(v[i]);
  sort(unzip.begin(), unzip.end());
  unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
  for (int i = 0; i < (int) unzip.size(); i++) zip[unzip[i]] = i;
}

class StronglyConnectedComponents {
private:
  int siz;
  int itr = 0;
  vector<bool> done;
  vector<int> order, comp;
  void dfs (int cur, const vector<vector<int>>& g) {
    done[cur] = true;
    for (int nxt : g[cur]) {
      if (!done[nxt]) dfs(nxt, g);
    }
    order.push_back(cur);
  }
  void rdfs (int cur, const vector<vector<int>>& rg) {
    done[cur] = true;
    comp[cur] = itr;
    for (int nxt : rg[cur]) {
      if (!done[nxt]) rdfs(nxt, rg);
    }
  }
  void build (const vector<vector<int>>& g, const vector<vector<int>>& rg) {
    for (int i = 0; i < siz; ++i) if (!done[i]) dfs (i, g);
    reverse(order.begin(), order.end());
    fill(done.begin(), done.end(), false);
    for (int i = 0; i < siz; ++i) if (!done[order[i]]) rdfs (order[i], rg), ++itr;
  }

public:
  StronglyConnectedComponents(const vector<vector<int>>& g, const vector<vector<int>>& rg, const int& n) : siz(n), done(siz, false), comp(siz, -1) {
    build(g, rg);
  }
  bool  has_cycle() { return *max_element(comp.begin(), comp.end()) < siz - 1; }
  vector<int> get_comp() { return comp;}
};
using SCC = StronglyConnectedComponents;

int dfs (int cur, vector<set<int>>& g, vector<int>& val, vector<int>& vis) {
  if (vis[cur]) return val[cur];
  vis[cur] = 1;
  for (int nxt : g[cur]) {
    val[cur] = max(val[cur], dfs(nxt, g, val, vis));
  }
  return val[cur];
}

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> all;
  vector<int> b(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i] >> c[i];
    all.push_back(b[i]);
    all.push_back(c[i]);
  }
  compress(all);
  for (int i = 0; i < m; i++) {
    b[i] = zip[b[i]];
    c[i] = zip[c[i]];
  }
  int nn = zip.size();
  vector<vector<int>> g(nn), rg(nn);
  for (int i = 0; i < m; i++) {
    g[b[i]].push_back(c[i]);
    rg[c[i]].push_back(b[i]);
  }
  SCC scc(g, rg, nn);
  vector<int> comp = scc.get_comp();
  int nnn = *max_element(comp.begin(), comp.end()) + 1;
  vector<set<int>> gg(nnn);
  vector<int> val(nn);
  for (int i = 0; i < m; i++) {
    val[comp[b[i]]] = max(val[comp[b[i]]], unzip[b[i]]);
    val[comp[c[i]]] = max(val[comp[c[i]]], unzip[c[i]]);
    if (comp[b[i]] == comp[c[i]]) continue;
    gg[comp[b[i]]].insert(comp[c[i]]);
  }
  vector<int> vis(nnn);
  dfs(0, gg, val, vis);
  long long ans = 1LL * n * (n + 1) / 2;
  set<int> app;
  for (auto x : b) {
    if (app.count(x)) continue;
    app.insert(x);
    int y = unzip[x];
    ans -= y;
    ans += val[comp[x]];
  }
  cout << ans << endl;
  return 0;
}
0