#include #ifndef DUMP #define DUMP(...) (void)0 #endif using namespace std; struct graph { struct edge { int src, dst; int operator-(int v) const { return src ^ dst ^ v; } }; int n, m; vector edges; vector>> adj; graph(int _n = 0) : n(_n), m(0), adj(n) {} int add(const edge& e, bool directed = false) { edges.push_back(e); adj[e.src].emplace_back(m, e.dst); if (not directed) adj[e.dst].emplace_back(m, e.src); return m++; } }; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n, m; cin >> n >> m; graph g(n); while (m--) { int b, c; cin >> b >> c; --b, --c; g.add({c, b}, true); } int64_t res = 0; vector vis(n); for (int s = n; s--;) { if (vis[s]) continue; auto dfs = [&](auto&& self, int v) -> void { res += s + 1; vis[v] = true; for (auto [id, u] : g.adj[v]) { if (vis[u]) continue; self(self, u); } }; dfs(dfs, s); } cout << res << '\n'; }