poj1022

//============================================================================
// 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值