POJ 1830 开关问题

高斯消元

首先想到状压搜索,但是会T
然后我们考虑到一个开关的最后状态,与它的开始状态与所有与它有关操作的异或和有关
因为每个开关只能操作一次,我们可以把每个开关看作是一个元,这些元之间用异或关系连接起来

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int T, a[100], n;
int main() {
	cin >> T;
	while(T--) {
		cin >> n;
		memset(a, 0, sizeof(a));
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		for(int i = 1; i <= n; i++){
			int t;
			cin >> t;
			a[i] ^= t;
			a[i] |= (1 << i);
		}
		int x, y;
		while(cin >> x >> y) {
			if(!x && !y) break;
			a[y] |= (1 << x);
		}
		int ans = 1;
		for(int i = 1; i <= n; i++) {
			for(int j = i + 1; j <= n; j++) {
				if(a[j] > a[i]) swap(a[i], a[j]);
			}
			if(!a[i]) {ans = (1<<(n - i + 1)); break;}
			if(a[i] == 1) {ans = 0;break;}
			for(int k = n; k; k--) {
				if((a[i] >> k) & 1) {
					for(int j = 1; j <= n; j++) {
						if(i !=j &&((a[j]>>k) & 1)) a[j] ^= a[i];
					}
					break;
				}
			}
		}
		if(!ans) printf("Oh,it's impossible~!!\n");
		else cout << ans << endl;
	}
	return 0;
}
posted @ 2018-03-13 21:57  Mr_Wolfram  阅读(185)  评论(0编辑  收藏  举报