zoj3209
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int INF=6000;
const int max_size = 1000*501;
int ygx_ans;
const int maxint = 0x3f3f3f3f;
int L[max_size], R[max_size], U[max_size], D[max_size], CH[max_size], RH[max_size];
int S[1005], O[1005];//S记录的是一列的sum,用于H函数,O记录选择的行坐标。
int head, size;
void remove(const int &c) {
//remove column c and all row i that A[i][c] == 1
L[R[c]] = L[c];
R[L[c]] = R[c];
//remove column c;
for (int i = D[c]; i != c; i = D[i]) {
//remove i that A[i][c] == 1
for (int j = R[i]; j != i; j = R[j]) {
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[CH[j]];
//decrease the count of column C[j];
}
}
}
void resume(const int &c) {
for (int i = U[c]; i != c; i = U[i]) {
for (int j = L[i]; j != i; j = L[j]) {
++S[CH[j]];
U[D[j]] = j;
D[U[j]] = j;
}
}
L[R[c]] = c;
R[L[c]] = c;
}
void dfs(int k) {
//cout << "new DFS" << endl;
if(k>=ygx_ans) return;
if (R[head] == head) {
//One of the answers has been found.
// return true;
ygx_ans=k;
return;
}
int s(maxint), c;
//out(head);
for (int t = R[head]; t != head; t = R[t]) {
//out(t); out(R[t]);
//select the column c which has the fewest number of element.
if (S[t] < s) {
s = S[t];
c = t;
}
}
//cout << "remove " << c << endl;
remove(c);
for (int i = D[c]; i != c; i = D[i]) {
for (int j = R[i]; j != i; j = R[j]) {
remove(CH[j]);
}
//if (dfs(k+1)) {
// return true;
dfs(k+1);
// }
for (int j = L[i]; j != i; j = L[j]) {
resume(CH[j]);
}
}
resume(c);
//return false;
}
int node(int up, int down, int left, int right) {
U[size] = up, D[size] = down;
L[size] = left, R[size] = right;
D[up] = U[down] = R[left] = L[right] = size;
return size++;
}
void init(int M) {
size = 0;
head = node(0, 0, 0, 0);
for (int j = 1; j <= M; ++j) {
node(j, j, L[head], head);
CH[j] = j, S[j] = 0;
}
}
int main(){
int t,n,m,p,a,b,c,d;
scanf("%d",&t);
while(t--){
ygx_ans=INF;
scanf("%d%d%d",&n,&m,&p);
init(n*m);
for(int i=0; i scanf("%d%d%d%d",&a,&b,&c,&d);
int row=-1;
for(int ii=a; ii for(int jj=b; jj CH[size]=ii*m+jj+1,++S[ii*m+jj+1];
if(row==-1){
row=node(U[CH[ii*m+jj+1]],CH[ii*m+jj+1],size,size);
RH[row]=i;
}
else{
int k=node(U[CH[ii*m+jj+1]],CH[ii*m+jj+1],L[row],row);
RH[k]=i;
}
}
}
}
for(int i=1; i<=m*n; ++i)
if(!S[i]) goto L;
dfs(0);
L:
if(ygx_ans!=INF)
printf("%d/n",ygx_ans);
else
printf("-1/n");
}
}