//============================================================================
// Name : 1022.cpp
// Author : sww
// Version : 1.0
// Time : 2013-11-09 12:47:37
//===========================================================================
#include <stdio.h>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#define N 100
#define M 8
using namespace std;
int myreverse[] = { 1, 0, 3, 2, 5, 4, 7, 6 };
int data[N][M];
int value[N];
int dis[M];
int pos[N][M];
bool cal[N];
int n;
set<int> setNumber;
map<int, int> valueToColumn;
bool check() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < M; j++) {
int other = data[i][j];
if (other > 0) {
if (data[valueToColumn[other]][myreverse[j]] != value[i]) {
return false;
}
}
}
}
return true;
}
void putInSet(int i) {
setNumber.insert(i);
int colum = valueToColumn[i];
for (int j = 0; j < M; j++) {
int next = data[colum][j];
if (next > 0) {
if (setNumber.find(next) == setNumber.end()) {
putInSet(next);
}
}
}
}
bool single() {
setNumber.clear();
putInSet(value[0]);
return setNumber.size() == n;
}
void calDistance(int i) {
setNumber.insert(i);
int colum = valueToColumn[i];
for (int j = 0; j < M; j++) {
int next = data[colum][j];
if (next > 0) {
if (setNumber.find(next) == setNumber.end()) {
int nc = valueToColumn[next];
for (int k = 0; k < M; k++) {
pos[nc][k] = pos[colum][k];
}
pos[nc][j]++;
dis[j] = max(dis[j], pos[nc][j]);
calDistance(next);
}
}
}
}
void calDistance() {
memset(dis, 0, sizeof(dis));
memset(pos[0], 0, sizeof(pos[0]));
memset(cal, 0, sizeof(cal));
setNumber.clear();
calDistance(value[0]);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &value[i]);
valueToColumn[value[i]] = i;
for (int j = 0; j < M; j++) {
scanf("%d", &data[i][j]);
}
}
if (check() && single()) {
calDistance();
int res = 1;
for (int i = 0; i < M; i += 2) {
int ch = dis[i] + dis[i + 1] + 1;
res *= ch;
}
printf("%d\n", res);
} else {
puts("Inconsistent");
}
}
return 0;
}
poj1022
最新推荐文章于 2020-08-21 11:43:45 发布