제출 #31097

#제출 시각아이디문제언어결과실행 시간메모리
31097RezwanArefin01Port Facility (JOI17_port_facility)C++14
22 / 100
6000 ms53280 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1e6 + 10;
vector<int> adj[maxn]; 
int col[maxn]; 

struct range{
	int l, r; 
	bool operator < (const range &p) const {
		return l == p.l ? r < p.r : l < p.l;
	}
} v[maxn];

bool cant(int i, int j) {
	return (v[i].l < v[j].l && v[j].l < v[i].r && v[j].r > v[i].r);
}

bool bfs(int r) {
	col[r] = 0; 
	queue<int> Q;
	Q.push(r); 
	while(!Q.empty()) {
		int u = Q.front(); Q.pop(); 
		for(int v : adj[u]) {
			if(col[v] == -1) {
				col[v] = col[u] ^ 1; 
				Q.push(v);
			} else if(col[v] == col[u]) return 0;
		}
	} return 1; 
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
	freopen("in", "r", stdin);
#endif
	int n; cin >> n; 
	for(int i = 0; i < n; i++) 
		cin >> v[i].l >> v[i].r; 
	sort(v, v+n);

	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(cant(i, j)) {
				adj[i].push_back(j);
				adj[j].push_back(i); 
			}
		}
	} int comp = 0;
	memset(col, -1, sizeof col);
	for(int i = 0; i < n; i++) {
		if(col[i] == -1) {
			comp++;
			if(!bfs(i)) return cout << 0 << endl, 0; 
			//for(int j=0; j < n; j++) cout << col[i] <<" "; cout << endl;
		}
	}
	ll ans = 1; 
	for(int i = 0; i < comp; i++) {
		ans = (ans * 2) % ll(1e9 + 7); 
	} cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...