結果
| 問題 |
No.1194 Replace
|
| コンテスト | |
| ユーザー |
やむなく
|
| 提出日時 | 2020-08-22 14:11:57 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,399 bytes |
| コンパイル時間 | 1,912 ms |
| コンパイル使用メモリ | 186,636 KB |
| 実行使用メモリ | 127,008 KB |
| 最終ジャッジ日時 | 2024-10-15 08:27:45 |
| 合計ジャッジ時間 | 12,141 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 1 TLE * 7 -- * 19 |
ソースコード
//
// Created by yamunaku on 2020/08/22.
//
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;
map<int, vector<int>> e;
map<int, vector<int>> ne;
map<int, int> id;
map<int, int> col;
void dfs(int x, int &num){
id[x] = -1;
for(auto &nx : e[x]){
if(id.find(nx) == id.end()){
dfs(nx, num);
}
}
id[x] = num++;
}
void dfs2(int x, int c){
col[x] = c;
for(auto &nx : ne[x]){
if(col.find(nx) == col.end()){
dfs2(nx, c);
}
}
}
int main(){
//CFS;
ll n;
int m;
cin >> n >> m;
vi b(m), c(m);
rep(i, m){
cin >> b[i] >> c[i];
e[b[i]].push_back(c[i]);
ne[c[i]].push_back(b[i]);
}
int sz = 0;
for(auto &p:e){
if(id.find(p.first) == id.end()){
dfs(p.first, sz);
}
}
ll ans = n * (n + 1) / 2;
vi a(sz);
map<int,int> ma;
for(auto &p : id){
a[p.second] = p.first;
ans -= p.first;
}
reverse(all(a));
rep(i, sz){
if(col.find(a[i]) == col.end()){
dfs2(a[i], a[i]);
}
}
rep(i, sz){
ma[col[a[i]]] = max(ma[col[a[i]]], a[i]);
}
map<int, vector<int>> ee;
rep(i, m){
ee[col[b[i]]].push_back(col[c[i]]);
}
map<int, int> f;
per(i, sz){
int cc = col[a[i]];
if(f[cc]) continue;
f[cc] = true;
for(auto &nx : ee[cc]){
ma[cc] = max(ma[cc], ma[nx]);
}
}
rep(i, sz){
ans += ma[col[a[i]]];
}
cout << ans << endl;
return 0;
}
やむなく