randomlized 2-SAT

· · 题解

本篇题解非正解,仅供拓展学习。

首先转化题目:我们有 2n 个逻辑变量 x_1\sim x_{2n},其中 x_{2i-1},x_{2i} 有且仅有一个 1。

此外,题目还会给你 m 组限制 (a,c),表示 x_a,x_c 必有一个为 0。

你需要构造一组 x,或者输出 -1 表示无解。

这道题显然可以用强连通做,但是这里要讲另一种做法。

注:实际上,这种方法正常最多只能获得 93pts,但这个解法仍有启发性,在平时的比赛中比较有用。

我们充分发挥人类智慧。

考虑这样的一个算法:

  1. 随机生成一组 x。

  2. 重复执行下面的语句,直到即将超时或找到一组合法的 x:

  1. 如果找到了合法的 x,则输出结果;否则输出 NIE。

其中,这个算法的期望时间复杂度可以参照k-SAT 和约束满足问题的概率算法(A Probabilistic Algorithm for k-SAT and Constraint Satisfaction Problems) - 知乎。

代码写出来是这样的:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
ll seed;
int randint(int l,int r){
    mt19937_64 rand(time(0) * ++seed);
    uniform_int_distribution<int> dis(l,r);
    return dis(rand);
}
const int N = 1e6 + 5;
int n,m,x[N],cnt[N];
struct node{int a,b = 0,c,d = 0;node(){}node(int w,int x,int y,int z){a = w,b = x,c = y,d = z,cnt[a]++,cnt[b]++,cnt[c]++,cnt[d]++;}}a[N];
clock_t st,en;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
    seed = rand() * (rand() & 1 ? 1 : -1) * rand();
    cin >> n >> m;
    n *= 2;
    for(int i = 1;i <= m;i++)cin >> a[i].a >> a[i].c,cnt[a[i].a]++,cnt[a[i].c]++;
    for(int i = 1;i <= n;i++){
        if(i & 1)a[++m] = node(i,0,i + 1,0);
        else a[++m] = node(i,1,i - 1,1);
    }
    random_shuffle(a + 1,a + m + 1);
    for(int i = 1;i <= m;i++){
        if(cnt[a[i].a] == 1 || cnt[a[i].c] == 1)a[i] = node(1,0,1,1);
    }
    for(int i = 1;i <= n;i++)x[i] = randint(0,1);
    clock_t st = clock();
    while(double(clock() - st) / CLOCKS_PER_SEC < 0.97){
        int pos = -1;
        for(int i = 1;i <= m;i++){
            if(!(x[a[i].a] == a[i].b || x[a[i].c] == a[i].d)){pos = i;break;}
        }
        if(pos == -1){
            for(int i = 1;i <= n;i++){
                if(x[i])cout << i << endl;
            }
            return 0;
        }
        int tmp = randint(1,cnt[a[pos].a] + cnt[a[pos].c]) <= cnt[a[pos].c] ? a[pos].a : a[pos].c;
        x[tmp] = !x[tmp];
    }
    cout << "NIE";
}

直接交上去大概率只能获得 70pts,所以考虑优化。

不难发现,我们如果想要优化的话,就需要使循环(实际上这个过程被称为随机游走)的次数增加。

观察到以下代码。

for(int i = 1;i <= m;i++){
    if(!(x[a[i].a] == a[i].b || x[a[i].c] == a[i].d)){pos = i;break;}
}

我们实际上是可以利用两个 bitset 让时间复杂度减少。具体来说,我们用两个 bitset,分别存储满足 x[a[i].a] != a[i].b 和 x[a[i].c] != a[i].d 的 i 的集合。此时 pos 就等于 (bit1 & bit2)._Find_first()。

接下来,当 x[tmp] = !x[tmp] 的时候,我们就相当于让 bit1 ^= vis1[tmp],bit2 ^= vis2[tmp],其中 vis1,vis2 分别是两个 bitset 数组,第 p 个 bitset 分别存储着 a[i].a = p 的 i 的集合和 a[i].c = p 的 i 的集合。

因此我们循环的时间复杂度就降到了 O(\frac{m+2n}{\omega}),其中 \omega=64。

接下来考虑消去分子上的 2n。

我们发现,对于这 2n 个形如“x_{2i-1},x_{2i} 之中有且仅有一个 1”的表达式,我们可以做到 O(1) 维护(实际上 O(\log n) 跑出来的效果差不多)。具体做法我懒得讲了,大家可以看代码。

最后,经过玄学优化的代码平均可以跑 3.5\times 10^6 次,可以通过除了 #7 之外的所有数据点。

给一下 93pts 的代码:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
ll seed;
mt19937_64 rnd(time(0));
int randint(int l,int r){
    return (rnd() % (r - l + 1)) + l;
}
const int N = 2e4 + 5,T = 3;
int n,m,qwq1[N],qwq2[N],cnt1,cnt2;
vector<int> g1[N],g2[N];
bitset<N> bit1,bit2,vis1[312],vis2[312];
bitset<8005> x1,x2;
bool x[16005];
struct node{int a,b = 0,c,d = 0;node(){}node(int w,int x,int y,int z){a = w,b = x,c = y,d = z;}}a[N];
clock_t stqwq,en;
set<int> st;
int main(){
//  ios::sync_with_stdio(false);
//  cin.tie(0);
//  cout.tie(0);
    srand(time(0));
    seed = rand() * (rand() & 1 ? 1 : -1) * rand();
    int cnt = 0;
    cin >> n >> m;
    n *= 2;
    for(int i = 1;i <= m;i++)cin >> a[i].a >> a[i].c;
//  for(int i = 1;i <= n;i++){
//      if(i & 1)a[++m] = node(i,0,i + 1,0);
//      else a[++m] = node(i,1,i - 1,1);
//  }
    for(int i = 1;i <= n;i++)x[i] = (rnd() & 1);
    for(int i = 1;i * 2 <= n;i++){
        if(x[i * 2 - 1] == x[i * 2])st.insert(i);
    }
//  assert(n / 2 <= 1000);
    clock_t stqwq = clock();
    for(int i = 1;i <= m;i++){
        g1[a[i].a].push_back(i);
        g2[a[i].c].push_back(i);
    }
    for(int i = 1;i <= n;i++){
        if(g1[i].size() > 312){
            qwq1[i] = ++cnt1;
            for(auto j : g1[i])vis1[cnt1].set(j);
        }
        if(g2[i].size() > 312){
            qwq2[i] = ++cnt2;
            for(auto j : g2[i])vis2[cnt2].set(j);
        }
    }
    for(int i = 1;i <= m;i++){
        if(x[a[i].a] != a[i].b)bit1.set(i);
    }
    for(int i = 1;i <= m;i++){
        if(x[a[i].c] != a[i].d)bit2.set(i);
    }
    int qwqqwq = 0;
    int tn = n / 2;
    while(double(clock() - stqwq) < 0.98 * CLOCKS_PER_SEC){
        qwqqwq++;
        int pos = (bit1 & bit2)._Find_first();
        int pos1 = (st.size() ? *st.begin() : -1);
//      for(int i = 1;i <= n;i++)cout << x[i] << " ";cout << endl;
//      for(int i = 1;i <= n / 2;i++)cout << x1[i] << " ";cout << endl;
//      for(int i = 1;i <= n / 2;i++)cout << x2[i] << " ";cout << endl;
//      system("pause");
//      cout << pos << " " << pos1 << endl;
        if(pos == N && pos1 == -1){
            for(int i = 1;i <= n;i++){
                if(x[i])cout << i << endl;
            }
            return 0;
        }
        int tmp = 0;
        if((pos != N && pos1 == -1) || (pos != N && ~pos1 && (rnd() % T) == 0))tmp = (rnd() % T ? a[pos].a : a[pos].c);
        else tmp = (rnd() & 1 ? (pos1 << 1) : (pos1 << 1) - 1);
        x[tmp] = !x[tmp];
        if((tmp & 1)){
            if(x[tmp] != x[tmp + 1])st.erase((tmp + 1) >> 1);
            else st.insert((tmp + 1) >> 1);
        }else{
            if(x[tmp] != x[tmp - 1])st.erase((tmp + 1) >> 1);
            else st.insert((tmp + 1) >> 1);
        }
        if(!qwq1[tmp]){
            for(auto i : g1[tmp])bit1.flip(i);
        }else{
            bit1 ^= vis1[qwq1[tmp]];
        }
        if(!qwq2[tmp]){
            for(auto i : g2[tmp])bit2.flip(i);
        }else{
            bit2 ^= vis2[qwq2[tmp]];
        }
    }
//  cout << qwqqwq << endl;
    cout << "NIE";
}

既然这篇题解发了出来,说明我已经 AC 了。

那具体是什么方法 AC 的呢?

猜测题目大概率是随机生成数据的,所以我们上面代码 g2 和 g1 两个 vector 的大小不会太大,所以我们没必要用 bitset,可以用 set 来维护。具体方法不谈。

最后通过调参数 + 整合上面的代码得以通过。AC record。