제출 #1223284

#제출 시각아이디문제언어결과실행 시간메모리
1223284rythm_of_knightPort Facility (JOI17_port_facility)C++17
0 / 100
23 ms47428 KiB
#include <bits/stdc++.h> #define ar array #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; const int N = 1e6 + 6, mod = 1e9 + 7; vector <int> g[N], tp[N]; int f[N], sz[N], d[N], done[N], w[N << 1], p[N << 1]; set <int> nxt; int father(int x) { if (f[x] != f[f[x]]) return f[x] = father(f[x]); return f[x]; } void dsu(int a, int b) { int sa = a, sb = b; a = father(a), b = father(b); if (a == b) return; // cout << sa << ' ' << sb << '\n' << flush; g[sa].push_back(sb); g[sb].push_back(sa); if (sz[a] < sz[b]) swap(a, b); if (!tp[b].empty() && !tp[a].empty()) { if (tp[a].back() < tp[b].back()) nxt.erase(-tp[a].back()); else nxt.erase(-tp[b].back()); } for (int i : tp[b]) tp[a].push_back(i); sort(all(tp[a])); f[b] = a; sz[a] += sz[b]; } void dfs(int x, int par) { d[x] = d[par] + 1; for (int y : g[x]) if (y != par) dfs(y, x); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int m = n << 1; for (int i = 1; i <= n; i++) { f[i] = i, sz[i] = 1; int a, b; // cout << i << ": " << flush; cin >> a >> b; tp[i] = {a}; p[a] = 0; p[b] = a; w[a] = w[b] = i; } for (int cur = 1; cur <= m; cur++) { if (!p[cur]) { nxt.insert(-cur); } else { int nd = - m - 1; while (!nxt.empty()) { auto nw = *nxt.upper_bound(nd); nw *= -1; if (nw <= p[cur]) break; dsu(w[nw], w[cur]); nd = -nw; } int par = father(w[cur]); if (tp[par].back() == p[cur]) { nxt.erase(-p[cur]); tp[par].pop_back(); if (!tp[par].empty()) nxt.insert(-tp[par].back()); } } } int com = 0; for (int i = 1; i <= n; i++) { if (!d[i]) { dfs(i, 0); com++; } } vector <int> ps[2]; for (int i = 1; i <= m; i++) { if (!p[i]) { ps[d[w[i]] & 1].push_back(i); } else { int b = d[w[i]] & 1; if (!ps[b].empty() && ps[b].back() > p[i]) return cout << 0, 0; ps[b].pop_back(); } } int ans = 1; for (int i = 1; i <= com; i++) (ans <<= 1) %= mod; cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...