結果

問題 No.1194 Replace
ユーザー oteraotera
提出日時 2020-08-25 06:55:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 3,702 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 145,608 KB
実行使用メモリ 87,704 KB
最終ジャッジ日時 2024-04-24 04:08:10
合計ジャッジ時間 8,439 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 236 ms
58,888 KB
testcase_01 AC 271 ms
62,284 KB
testcase_02 AC 204 ms
51,440 KB
testcase_03 AC 166 ms
44,240 KB
testcase_04 AC 263 ms
62,400 KB
testcase_05 AC 243 ms
58,216 KB
testcase_06 AC 215 ms
54,712 KB
testcase_07 AC 363 ms
87,684 KB
testcase_08 AC 404 ms
87,676 KB
testcase_09 AC 365 ms
87,704 KB
testcase_10 AC 358 ms
87,704 KB
testcase_11 AC 367 ms
87,576 KB
testcase_12 AC 364 ms
87,652 KB
testcase_13 AC 210 ms
40,248 KB
testcase_14 AC 185 ms
34,000 KB
testcase_15 AC 146 ms
35,120 KB
testcase_16 AC 209 ms
40,088 KB
testcase_17 AC 137 ms
32,192 KB
testcase_18 AC 143 ms
29,292 KB
testcase_19 AC 211 ms
41,296 KB
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 AC 57 ms
17,848 KB
testcase_24 AC 19 ms
9,068 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 74 ms
16,248 KB
testcase_27 AC 14 ms
5,664 KB
testcase_28 AC 37 ms
9,692 KB
testcase_29 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<typename T>
vector<T> compress(vector<T> A){
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    return A;
}

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  vector<vector<int>> gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector<vector<int>> &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};

void solve() {
	  int n, m; cin >> n >> m;
    vector<int> x;
    vector<int> b(m), c(m);
    rep(i, m) {
        cin >> b[i] >> c[i];
        x.pb(b[i]), x.pb(c[i]);
    }
    auto comp = compress(x);
    int sz = (int)comp.size();
    vector<vector<int>> g(sz, vector<int>());
    rep(i, m) {
        int be = lower_bound(all(comp), b[i]) - comp.begin();
        int ce = lower_bound(all(comp), c[i]) - comp.begin();
        g[be].pb(ce);
    }
    StronglyConnectedComponents<vector<vector<int>>> scc(g);
    vector<vector<int>> t;
    scc.build(t);
    vector<int> dp(sz, 0);
    rep(i, sz) {
        chmax(dp[scc[i]], comp[i]);
    }
    for(int i = (int)t.size() - 1; i >= 0; -- i) {
        for(int j: t[i]) {
            chmax(dp[i], dp[j]);
        }
    }
    int ans = n * (n + 1) / 2;
    rep(i, sz) {
        ans += dp[scc[i]] - comp[i];
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
0