結果
問題 | No.1194 Replace |
ユーザー | 👑 emthrm |
提出日時 | 2020-08-22 15:06:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 865 ms / 2,000 ms |
コード長 | 4,194 bytes |
コンパイル時間 | 3,525 ms |
コンパイル使用メモリ | 225,944 KB |
最終ジャッジ日時 | 2025-01-13 09:09:57 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#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; }