#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #include int main() { constexpr int lim = 200000; int n; cin >> n; auto g = vec(uns, lim, 0); auto r = vec(uns, lim, 0); auto h = vec(uns, n); auto w = vec(uns, n); for (int i = 0; i < n; ++i) { cin >> h[i] >> w[i]; --h[i]; --w[i]; g[h[i]].push_back(w[i]); r[w[i]].push_back(h[i]); } atcoder::dsu dsu(lim); auto in = vec(0, lim); auto out = vec(0, lim); for (int i = 0; i < lim; ++i) { for (auto v : g[i]) { ++out[i]; --out[v]; ++in[v]; --in[i]; dsu.merge(i, v); } } for (int i = 0; i < n; ++i) { if (!dsu.same(h[0], h[i]) || !dsu.same(w[0], w[i])) { cout << 0 << endl; return 0; } } if (count(begin(out), end(out), 0) == lim) { set ans; for (int i = 0; i < n; ++i) { ans.insert(h[i]); ans.insert(w[i]); } cout << size(ans) << endl; return 0; } if (1 < count(begin(in), end(in), 1) || 1 < count(begin(out), end(out), 1)) { cout << 0 << endl; return 0; } if (count(begin(in), end(in), 0) != lim - 2 || count(begin(out), end(out), 0) != lim - 2) { cout << 0 << endl; return 0; } cout << 1 << endl; }