이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int)(x).size())
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y > x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int) 1e6 + 123;
const ll MOD = (ll) 1e9 + 7;
int n;
int a[N], b[N];
vector<int> g[N];
int col[N];
void dfs(int v, int cc = 1) {
col[v] = cc;
for (int u : g[v]) {
if (!col[u]) {
dfs(u, 3 - cc);
}
if (col[u] != 3 - cc) {
cout << "0\n";
exit(0);
}
}
}
bool closed[N];
pair<int, bool> what[2 * N];
void run() {
cin >> n;
rep(i, 0, n) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
what[a[i]] = {i, 0};
what[b[i]] = {i, 1};
}
int e = 0;
vector<int> st, tmp;
rep(i, 0, 2 * n) {
int v = what[i].first;
if (what[i].second) {
while (1) {
int t = st.back();
st.pop_back();
if (t == v) {
break;
}
if (!closed[t]) {
tmp.pb(t);
}
}
for (int u : tmp) {
g[u].pb(v);
g[v].pb(u);
e++;
}
while (sz(tmp)) {
int u = tmp.back();
tmp.pop_back();
st.pb(u);
}
closed[v] = 1;
} else {
st.pb(v);
}
}
ll ans = 1;
rep(i, 0, n) {
if (!col[i]) {
dfs(i);
ans = (ans * 2) % MOD;
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |