P1657 选书洛谷上的原题,but可达只有 60 分,代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 18;
bool a[MAXN + 5][MAXN + 5],used[MAXN + 5];
int n,ans;
void dfs(int k){
if(k == n + 1){
ans++;
return;
}else{
for(int i = 1;i <= n;i++){
if(a[i][k] && !used[i]){
used[i] = true;
dfs(k + 1);
used[i] = false;
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> n;
if(n == 0){
cout << 0 << endl;
return 0;
}
memset(a,false,sizeof(a));
for(int i = 1,k;i <= n;i++){
cin >> k;
a[i][k] = true;
cin >> k;
a[i][k] = true;
}
dfs(1);
cout << ans << endl;
return 0;
}
好像是什么我的输出是 0,但答案分别是 1024 和 5。