結果
問題 | No.1194 Replace |
ユーザー |
![]() |
提出日時 | 2020-08-22 17:23:57 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,923 ms / 2,000 ms |
コード長 | 1,669 bytes |
コンパイル時間 | 2,211 ms |
コンパイル使用メモリ | 188,324 KB |
実行使用メモリ | 98,560 KB |
最終ジャッジ日時 | 2024-10-15 11:16:43 |
合計ジャッジ時間 | 31,606 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector<ll> #define mat vector<vector<ll> > #define fi first #define se second typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; typedef long double ld; typedef complex<double> comp; const ll inf=1e9+7; const ll mod=998244353; const int MAX=300010; signed main(){ ll n,m;cin>>n>>m; map<ll,ll>idx; map<ll,ll>inv; map<ll,ll>con; vector<vector<ll> >G(2*m); ll now=0; vector<ll>q(m); rep(i,m){ ll b,c;cin>>b>>c; con[b]=b; con[c]=c; q[i]=c; if(idx.find(c)==idx.end()){ idx[c]=now; now++; } if(idx.find(b)==idx.end()){ idx[b]=now; now++; } inv[idx[c]]=c; inv[idx[b]]=b; G[idx[c]].pb(idx[b]); } sort(rall(q)); vector<ll>used(2*m); rep(i,m){ if(used[idx[q[i]]])continue; ll c=q[i]; queue<ll>q; used[idx[c]]=1; q.push(idx[c]); while(!q.empty()){ ll k=q.front(); q.pop(); //cout<<inv[k]<<' '<<c<<endl; for(auto e:G[k]){ if(used[e])continue; con[inv[e]]=max(con[inv[e]],c); used[e]=1; q.push(e); } } } ll ans=n*(n+1)/2; for(auto e:con){ ans+=e.se-e.fi; } cout<<ans<<endl; }