結果
問題 | No.1194 Replace |
ユーザー | snow39 |
提出日時 | 2020-08-22 15:27:37 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 915 ms / 2,000 ms |
コード長 | 2,574 bytes |
コンパイル時間 | 1,753 ms |
コンパイル使用メモリ | 121,056 KB |
実行使用メモリ | 103,352 KB |
最終ジャッジ日時 | 2024-04-23 09:47:13 |
合計ジャッジ時間 | 17,119 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 638 ms
66,016 KB |
testcase_01 | AC | 692 ms
69,448 KB |
testcase_02 | AC | 472 ms
57,992 KB |
testcase_03 | AC | 447 ms
50,312 KB |
testcase_04 | AC | 663 ms
69,564 KB |
testcase_05 | AC | 609 ms
65,128 KB |
testcase_06 | AC | 581 ms
61,620 KB |
testcase_07 | AC | 814 ms
103,216 KB |
testcase_08 | AC | 867 ms
103,084 KB |
testcase_09 | AC | 915 ms
103,176 KB |
testcase_10 | AC | 841 ms
103,228 KB |
testcase_11 | AC | 867 ms
103,352 KB |
testcase_12 | AC | 817 ms
103,352 KB |
testcase_13 | AC | 493 ms
36,412 KB |
testcase_14 | AC | 435 ms
29,216 KB |
testcase_15 | AC | 394 ms
36,520 KB |
testcase_16 | AC | 508 ms
37,144 KB |
testcase_17 | AC | 309 ms
33,220 KB |
testcase_18 | AC | 360 ms
27,112 KB |
testcase_19 | AC | 467 ms
39,492 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 | 140 ms
18,616 KB |
testcase_24 | AC | 38 ms
9,984 KB |
testcase_25 | AC | 11 ms
5,376 KB |
testcase_26 | AC | 150 ms
13,300 KB |
testcase_27 | AC | 17 ms
5,376 KB |
testcase_28 | AC | 52 ms
8,280 KB |
testcase_29 | AC | 6 ms
5,376 KB |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} /* example int n=scc.scc(); scc.build(n,g); (g:outside) */ struct StronglyConnectedComponents{ vector<int> used,cmp,vs; vector<vector<int>> ori; vector<vector<int>> v,rv; StronglyConnectedComponents(int n):used(n),cmp(n,-1),v(n),rv(n){} void add_edge(int x,int y){ v[x].push_back(y); rv[y].push_back(x); } void dfs(int x){ used[x]=true; for(auto to:v[x]) if(!used[to]) dfs(to); vs.push_back(x); } void rdfs(int x,int k){ cmp[x]=k; for(auto to:rv[x]) if(cmp[to]==-1) rdfs(to,k); } int scc(){ for(int i=0;i<used.size();i++) if(!used[i]) dfs(i); reverse(vs.begin(),vs.end()); int k=0; for(auto x:vs) if(cmp[x]==-1) rdfs(x,k++); ori.resize(k); for(int i=0;i<used.size();i++) ori[cmp[i]].push_back(i); return k;//k:size } void build(int k,vector<vector<int>> &g){ g.resize(k); for(int i=0;i<v.size();i++){ for(auto to:v[i]){ int x=cmp[i],y=cmp[to]; if(x!=y) g[x].push_back(y); } } } }; vector<vector<int>> g; vector<int> vis; vector<int> dp; vector<ll> B,C; void dfs(int now){ if(vis[now]) return; for(auto nex:g[now]){ dfs(nex); chmax(dp[now],dp[nex]); } vis[now]=1; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N,M; cin>>N>>M; B.resize(M); C.resize(M); rep(i,M) cin>>B[i]>>C[i]; vector<int> v; rep(i,M) v.push_back(B[i]); rep(i,M) v.push_back(C[i]); sort(all(v)); v.erase(unique(all(v)),v.end()); int V=v.size(); map<int,int> mp; rep(i,V) mp[v[i]]=i; StronglyConnectedComponents scc(V); rep(i,M){ scc.add_edge(mp[B[i]],mp[C[i]]); } int S=scc.scc(); scc.build(S,g); dp.resize(S,0); for(int i=0;i<V;i++) chmax(dp[scc.cmp[i]],v[i]); vis.resize(S,0); for(int i=0;i<S;i++){ if(vis[i]) continue; dfs(i); } ll ans=N*(N+1)/2; for(int i=0;i<V;i++){ ans-=v[i]; ans+=dp[scc.cmp[i]]; } cout<<ans<<endl; return 0; }