# BZOJ 4671 异或图 | 线性基 容斥 DFS

## 题面

Description

G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中, 否则这条边不在 G 中.

Input

Algorithm 1 Print a graph G = (V, E)
for i = 1 to n do
for j = i + 1 to n do
if G contains edge (i, j) then
print 1
else
print 0
end if
end for
end for
2 ≤ n ≤ 10,1 ≤ s ≤ 60.
Output

Sample Input
3
1
1
0
Sample Output
4

## 题解

（其中我最不会的居然是DFS! TAT）

n个点分成任意大小组的所有方案都可以DFS枚举出来。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}

const int N = 11, M = 61;
char s[N*N];
bool conn[M][N][N];
int n, m, bel[N];
ll fac[N], lb[M], ans;

void dfs(int u, int x){
if(u > n){
int sze = 0;
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
if(bel[i] != bel[j]){
ll val = 0;
for(int k = 1; k <= m; k++)
if(conn[k][i][j]) val |= 1LL << (k - 1);
for(int k = 1; k <= sze; k++)
if((val ^ lb[k]) < val) val ^= lb[k];
if(val) lb[++sze] = val;
}
ans += fac[x] * (1LL << (m - sze));
return;
}
for(int i = 1; i <= (x + 1); i++)
bel[u] = i, dfs(u + 1, x + (i > x));
}

int main(){

for(int k = 1; k <= m; k++){
scanf("%s", s + 1);
if(!n)
for(int i = 1, len = strlen(s + 1); !n; i++)
if(i * (i - 1) / 2 == len)
n = i;
for(int i = 1, p = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
conn[k][i][j] = s[p++] - '0';
}
fac[1] = 1;
for(int i = 2; i <= n; i++)
fac[i] = fac[i - 1] * (1 - i);
dfs(1, 0);
write(ans), enter;

return 0;
}

posted @ 2018-03-21 15:05  胡小兔  阅读(491)  评论(0编辑  收藏