可以说,这道题是一道无需优化的DFS,由于N最大只有20,连O(2^n)的复杂度都能过,因此,只需要DFS枚举每一个节点分配到哪一个子集即可。
因此就能得出代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define LL long long
#define N 20
using namespace std;
int n,ans=0,len1=0,len2=0,s1[N+5],s2[N+5],a[N+5][N+5];
int read()
{
int x=0,f=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') f=-1,ch=getchar();
while(ch>='0'&&ch<='9') (x*=10)+=ch-'0',ch=getchar();
return x*=f;
}
void write(int x)
{
if(x<