이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,long long>pii;
typedef array<int,5>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
struct DSU{
vector<int>e;
void init(int n){
e=vector<int>(n,-1);
}
int get(int x){
return e[x]<0?x:e[x]=get(e[x]);
}
bool sameSet(int x, int y){
return get(x)==get(y);
}
bool unite(int x, int y){
x=get(x); y=get(y);
if(x==y) return 1;
if(e[x]>e[y]) swap(x,y);
e[x]+=e[y];
e[y]=x;
return 0;
}
};
vector<int>adj[2005];
bool visited[2005];
const int mod=1e9+7;
void dfs(int index){
visited[index]=true;
for(auto it:adj[index]){
if(visited[it]) continue;
dfs(it);
}
}
void solve(){
int n;
cin >> n;
pii arr[n];
for(int x=0;x<n;x++){
cin >> arr[x].first >> arr[x].second;
}
sort(arr,arr+n);
DSU dsu;
dsu.init(2*n+5);
for(int x=0;x<n;x++){
for(int y=x+1;y<n;y++){
if(arr[y].first<arr[x].second&&arr[y].second>arr[x].second){
adj[x].push_back(y);
adj[y].push_back(x);
dsu.unite(x,y+n);
dsu.unite(x+n,y);
}
}
}
for(int x=0;x<n;x++){
if(dsu.sameSet(x,x+n)){
cout << 0;
return;
}
}
int counter=0;
for(int x=0;x<n;x++){
if(!visited[x]){
dfs(x);
counter++;
}
}
int ans=1;
for(int x=0;x<counter;x++){
ans=(ans*2)%mod;
}
cout << ans;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//freopen("in.txt","r",stdin);
//cin >> t;
while(t--){
solve();
}
}
# | 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... |