이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 1000001;
int n, col[N], p[N], sz[N], vis[N], mod = 1e9 + 7;
vector <pair <int, int> > a;
vector <int> f, v[N];
int vis_id;
void invert(int node){
if(vis[node] == vis_id) return;
vis[node] = vis_id;
col[node] ^= 1;
for(auto &i : v[node]){
invert(i);
}
}
int find(int x){
if(p[x] == x) return x;
return p[x] = find(p[x]);
}
void merge(int x, int y){
if(col[x] == -1 || col[y] == -1){
v[x].push_back(y);
v[y].push_back(x);
sz[find(x)] += sz[find(y)];
p[find(y)] = find(x);
if(col[x] == -1 && col[y] == -1){
col[x] = 0;
col[y] = 1;
return;
}
if(col[x] == -1) swap(x, y);
col[y] = col[x] ^ 1;
return;
}
if(find(x) == find(y)){
if(col[x] == col[y]){
cout << 0 << endl;
exit(0);
}
return;
}
if(col[x] == (col[y] ^ 1)){
sz[find(x)] += sz[find(y)];
p[find(y)] = find(x);
return;
}
if(sz[find(x)] < sz[find(y)]) swap(x, y);
vis_id++;
invert(y);
assert(col[x] != col[y]);
v[x].push_back(y);
v[y].push_back(x);
sz[find(x)] += sz[find(y)];
p[find(y)] = find(x);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(col, -1, sizeof col);
cin >> n;
a.resize(n);
f.resize(2 * n);
for(int i = 0 ; i < n ; i++){
cin >> a[i].first >> a[i].second;
a[i].first--; a[i].second--;
f[a[i].first] = f[a[i].second] = i;
p[i] = i;
sz[i] = 1;
}
vector <int> o, del(n);
for(int i = 0 ; i < 2 * n ; i++){
while(o.size() && del[o.back()]){
o.pop_back();
}
if(a[f[i]].first == i){
o.push_back(f[i]);
}
else{
del[f[i]] = 1;
int j = (int)o.size() - 1;
while(o[j] != f[i]){
if(del[o[j]] == 0){
merge(f[i], o[j]);
}
j--;
}
}
}
int ans = 1;
for(int i = 0 ; i < n ; i++){
if(find(i) == i){
ans *= 2;
if(ans >= mod) ans -= mod;
}
}
cout << ans << endl;
}
# | 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... |