题解:AT_arc226_a [ARC226A] Meeting Division
观察一下发现两个人的计数问题本质上可以拆成两步:
- 判定是否为二分图。不是二分图必然无解。其中这个图是将有相交的两个区间连边。
- 对于一个连通块有两种方案,设连通块个数为
t ,则方案数为2^t 。
对于第一步,区间图是弦图,判断是否存在奇环等价于是否存在点数为
对于第二步,考虑每个连通块一定是一个区间,设其为
瓶颈在于排序。复杂度
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 1e6;
using ll = long long;
constexpr ll mod = 998244353;
ll a[N + 5], n, p[N + 5];
struct Node {
int s, t;
bool operator < (const Node cpr) const {return t < cpr.t; }
}nd[N + 5];
ll qpow(ll x, ll y) {
ll ans = 1; while(y) {if(y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1;}
return ans;
}
vector<int> v;
int main() {
cin >> n;
for(int i = 1; i <= n; ++i) cin >> nd[i].s >> nd[i].t, --p[nd[i].t + 1], ++p[nd[i].s];
sort(nd + 1, nd + n + 1);
for(int i = 1; i <= n + n + 5; ++i) p[i] += p[i - 1];
for(int i = 1; i <= n + n + 5; ++i) if(p[i] > 2) {cout << 0; return 0;}
for(int i = 1; i <= n; ++i) {
while(!v.empty() && v.back() > nd[i].s) v.pop_back();
v.push_back(nd[i].t);
}
cout << qpow(2, v.size());
return 0;
}