a01sa01to's competitive programming library.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#include "../../../cpp/library/data-structure/digraph.hpp"
#include "../../../cpp/library/graph/scc.hpp"
#define PROBLEM "https://atcoder.jp/contests/abc296/tasks/abc296_e"
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i], a[i]--;
asalib::graph::digraph Graph(n);
rep(i, n) Graph.add_edge(i, a[i]);
auto scc = Graph.scc();
int ans = 0;
for (auto& v : scc) {
if (v.size() == 1 && a[v[0]] != v[0]) continue;
ans += v.size();
}
cout << ans << endl;
return 0;
}
#line 1 "tests/graph/scc/abc296-e.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#line 2 "library/data-structure/digraph.hpp"
#line 4 "library/data-structure/digraph.hpp"
#include <optional>
#line 6 "library/data-structure/digraph.hpp"
using namespace std;
#line 2 "library/_internal/graph-base.hpp"
#include <concepts>
#include <type_traits>
using namespace std;
namespace asalib {
namespace _internal {
class graph_base {};
class notweighted_graph_base: public graph_base {};
class weighted_graph_base: public graph_base {};
template<typename T>
concept is_graph = is_base_of_v<graph_base, T>;
template<typename T>
concept notweighted_graph = is_base_of_v<notweighted_graph_base, T>;
template<typename T>
concept weighted_graph = is_base_of_v<weighted_graph_base, T>;
using adjlist_t = vector<vector<pair<size_t, size_t>>>;
using edgelist_t = vector<pair<size_t, size_t>>;
} // namespace _internal
} // namespace asalib
#line 2 "library/data-structure/graph.hpp"
#line 6 "library/data-structure/graph.hpp"
using namespace std;
#line 9 "library/data-structure/graph.hpp"
namespace asalib {
namespace graph {
class graph: public _internal::notweighted_graph_base {
public:
graph(): n_vertex(0), n_edge(0) {}
explicit graph(size_t n_vertex): n_vertex(n_vertex), n_edge(0) {
adj_list.reserve(n_vertex);
adj_list.resize(n_vertex);
}
void add_edge(size_t v1, size_t v2) {
assert(0 <= v1 && v1 < n_vertex);
assert(0 <= v2 && v2 < n_vertex);
adj_list[v1].push_back({ v2, n_edge });
adj_list[v2].push_back({ v1, n_edge });
edge_list.push_back({ v1, v2 });
++n_edge;
}
// (v1, v2)
pair<size_t, size_t> get_edge(size_t edgeId) const {
assert(0 <= edgeId && edgeId < n_edge);
return edge_list[edgeId];
}
// (v2, edgeId)
vector<pair<size_t, size_t>> get_adj(size_t vertex) const {
assert(0 <= vertex && vertex < n_vertex);
return adj_list[vertex];
}
// ---------- prototype ---------- //
optional<pair<vector<size_t>, vector<size_t>>> cycle() const;
bool is_connected() const;
optional<pair<vector<size_t>, vector<size_t>>> eulerian_walk() const;
private:
size_t n_vertex, n_edge;
asalib::_internal::adjlist_t adj_list;
asalib::_internal::edgelist_t edge_list;
};
} // namespace graph
} // namespace asalib
#line 10 "library/data-structure/digraph.hpp"
namespace asalib {
namespace graph {
class digraph: public _internal::notweighted_graph_base {
public:
digraph(): n_vertex(0), n_edge(0) {}
explicit digraph(size_t n_vertex): n_vertex(n_vertex), n_edge(0) {
adj_list.reserve(n_vertex);
adj_list.resize(n_vertex);
adj_list_rev.reserve(n_vertex);
adj_list_rev.resize(n_vertex);
underlying_graph = graph(n_vertex);
}
void add_edge(size_t from, size_t to) {
assert(0 <= from && from < n_vertex);
assert(0 <= to && to < n_vertex);
adj_list[from].push_back({ to, n_edge });
adj_list_rev[to].push_back({ from, n_edge });
edge_list.push_back({ from, to });
underlying_graph.add_edge(from, to);
++n_edge;
}
// (from, to)
pair<size_t, size_t> get_edge(size_t edgeId) const {
assert(0 <= edgeId && edgeId < n_edge);
return edge_list[edgeId];
}
// (to, edgeId)
vector<pair<size_t, size_t>> get_adj(size_t vertex) const {
assert(0 <= vertex && vertex < n_vertex);
return adj_list[vertex];
}
// (from, edgeId)
vector<pair<size_t, size_t>> get_adjrev(size_t vertex) const {
assert(0 <= vertex && vertex < n_vertex);
return adj_list_rev[vertex];
}
// ---------- prototype ---------- //
vector<vector<size_t>> scc() const;
optional<pair<vector<size_t>, vector<size_t>>> cycle() const;
bool is_connected() const;
optional<pair<vector<size_t>, vector<size_t>>> eulerian_walk() const;
private:
size_t n_vertex, n_edge;
asalib::_internal::adjlist_t adj_list, adj_list_rev;
asalib::_internal::edgelist_t edge_list;
graph underlying_graph;
};
} // namespace graph
} // namespace asalib
#line 2 "library/graph/scc.hpp"
#line 5 "library/graph/scc.hpp"
using namespace std;
#line 8 "library/graph/scc.hpp"
namespace asalib {
namespace graph {
vector<vector<size_t>> digraph::scc() const {
constexpr size_t Unvisited = -1;
vector<bool> visited(n_vertex, false);
size_t order_num = 0;
vector<size_t> ord2pt(n_vertex, Unvisited);
function<void(size_t)> visit1 = [&](size_t v) {
visited[v] = true;
for (auto [w, _] : adj_list[v]) {
if (!visited[w]) visit1(w);
}
ord2pt[order_num++] = v;
};
for (size_t v = 0; v < n_vertex; ++v) {
if (!visited[v]) visit1(v);
}
visited.assign(n_vertex, false);
size_t comp_num = 0;
vector<size_t> comp(n_vertex, Unvisited);
function<void(size_t)> visit2 = [&](size_t v) {
visited[v] = true;
for (auto [w, _] : adj_list_rev[v]) {
if (!visited[w]) visit2(w);
}
comp[v] = comp_num;
};
for (int i = n_vertex - 1; i >= 0; --i) {
if (!visited[ord2pt[i]]) visit2(ord2pt[i]), ++comp_num;
}
vector<vector<size_t>> res(comp_num);
for (size_t v = 0; v < n_vertex; ++v) {
res[comp[v]].push_back(v);
}
return res;
}
} // namespace graph
} // namespace asalib
#line 9 "tests/graph/scc/abc296-e.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc296/tasks/abc296_e"
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i], a[i]--;
asalib::graph::digraph Graph(n);
rep(i, n) Graph.add_edge(i, a[i]);
auto scc = Graph.scc();
int ans = 0;
for (auto& v : scc) {
if (v.size() == 1 && a[v[0]] != v[0]) continue;
ans += v.size();
}
cout << ans << endl;
return 0;
}