結果

問題 No.1194 Replace
ユーザー ChanyuhChanyuh
提出日時 2020-08-23 16:59:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,483 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 143,284 KB
実行使用メモリ 79,884 KB
最終ジャッジ日時 2024-04-23 18:07:28
合計ジャッジ時間 17,712 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 220 ms
50,320 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 269 ms
33,656 KB
testcase_14 AC 230 ms
26,092 KB
testcase_15 AC 217 ms
36,404 KB
testcase_16 AC 269 ms
34,948 KB
testcase_17 AC 190 ms
32,800 KB
testcase_18 AC 176 ms
25,604 KB
testcase_19 AC 276 ms
37,728 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 74 ms
18,740 KB
testcase_24 AC 25 ms
9,988 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 84 ms
11,496 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 AC 36 ms
7,532 KB
testcase_29 AC 6 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#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 Per(i,sta,n) for(int i=n-1;i>=sta;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 long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

struct SCC{
  vector<vector<int> > G,R,T,C;//T:強連結分解後のグラフ
  vector<int> vs,used,blg;
  SCC(int n):G(n),R(n),used(n),blg(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    R[v].emplace_back(u);
  }

  int size(){
    return T.size();
  }

  void dfs(int v){
    used[v]=1;
    for(int u:G[v])
      if(!used[u]) dfs(u);
    vs.emplace_back(v);
  }

  void rdfs(int v,int k){
    used[v]=1;
    blg[v]=k;
    C[k].emplace_back(v);
    for(int u:R[v])
      if(!used[u]) rdfs(u,k);
  }

  int build(){
    int n=G.size();
    for(int v=0;v<n;v++)
      if(!used[v]) dfs(v);

    fill(used.begin(),used.end(),0);
    int k=0;
    for(int i=n-1;i>=0;i--){
      if(!used[vs[i]]){
        T.emplace_back();
        C.emplace_back();
        rdfs(vs[i],k++);
      }
    }
    for(int v=0;v<n;v++)
      for(int u:G[v])
        if(blg[v]!=blg[u])
          T[blg[v]].push_back(blg[u]);

    for(int i=0;i<k;i++){
      sort(T[i].begin(),T[i].end());
      T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
    }
    return k;
  }
  int operator[](int k) const{return blg[k];};//頂点kの属する強連結成分
};

ll n;int m;
int num[200010];ll sco[200010];
ll v[200010];
vector<int> zaatu;
P e[200010];
ll ans=0;

void dfs(int s, vector<vector<int>> &G){
    if(v[s]!=0) return;
    v[s]=max(v[s],sco[s]);
    for(int t:G[s]){
        if(v[t]==0) dfs(t,G);
        v[s]=max(v[t],v[s]);
    }
    ans+=v[s]*(ll)num[s];
}

void solve(){
    cin >> n >> m;
    rep(i,m){
        int b,c;cin >> b >> c;
        zaatu.push_back(b);
        zaatu.push_back(c);
        e[i]=P(b,c);
    }
    sort(zaatu.begin(),zaatu.end());
    zaatu.erase(unique(zaatu.begin(),zaatu.end()),zaatu.end());
    int V=zaatu.size();
    SCC scc(V);
    // rep(i,V){
    //     cout << zaatu[i] << endl;
    // }
    rep(i,m){
        int u=lower_bound(zaatu.begin(),zaatu.end(),e[i].first)-zaatu.begin();
        int v=lower_bound(zaatu.begin(),zaatu.end(),e[i].second)-zaatu.begin();
        scc.add_edge(u,v);
    }
    int k=scc.build();
    ll d=0;
    rep(i,V){
        sco[scc[i]]=max(sco[scc[i]],(ll)zaatu[i]);
        num[scc[i]]+=1;
        d+=zaatu[i];
    }
    // rep(i,k){
    //     cout << i << " " << sco[i] << " " << num[i] << endl;
    // }
    vector<vector<int>> G=scc.T;
    rep(i,k){
        dfs(i,G);
    }
    cout << n*(n+1)/2+ans-d << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0