結果
| 問題 |
No.1813 Magical Stones
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2022-01-14 22:26:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 124 ms / 2,000 ms |
| コード長 | 1,948 bytes |
| コンパイル時間 | 839 ms |
| コンパイル使用メモリ | 82,836 KB |
| 最終ジャッジ日時 | 2025-01-27 11:48:05 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned int;
#define rep(i,n) for(int i=0; i<(n); i++)
pair<int, vector<int>> SCC(const vector<vector<int>>& E) {
int n = E.size();
vector<int> F(n, 0), St, I, perm, g;
vector<vector<int>> R(n), G, res;
rep(i, n) if (F[i] == 0) {
St.push_back(i);
while (St.size()) {
int p = St.back();
St.pop_back();
if (p < 0) { I.push_back(-1 - p); continue; }
if (F[p]) continue;
F[p] = -1;
St.push_back(-1 - p);
for (int e : E[p]) St.push_back(e);
}
}
rep(i, n) for (int e : E[i]) R[e].push_back(i);
reverse(I.begin(), I.end());
g.reserve(n); int gi = 0;
int res_n = 0;
for (int s : I) if (F[s] == -1) {
g.push_back(s);
F[s] = res_n;
while(gi < g.size()){
int p = g[gi++];
for (int e : R[p]) if (F[e] == -1) {
F[e] = res_n;
g.push_back(e);
}
}
res_n++;
}
return make_pair(res_n, move(F));
}
int main() {
int N; cin >> N;
int M; cin >> M;
vector<vector<int>> E(N);
rep(i,M){
int u,v; cin >> u >> v; u--; v--;
E[u].push_back(v);
}
auto [nsc,SCid] = SCC(E);
vector<int> typesc(nsc, 0);
rep(u,N) for(int v : E[u]) if(SCid[u] != SCid[v]){
typesc[SCid[u]] |= 1;
typesc[SCid[v]] |= 2;
}
if(nsc == 1){ cout << "0\n"; return 0; }
int typecnt[4] = {};
for(int t : typesc) typecnt[t]++;
int ans = max(typecnt[0] + typecnt[1], typecnt[0] + typecnt[2]);
cout << ans << endl;
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
} ios_do_not_sync_instance;
Nachia