您的位置:首页 > 其它

POJ 2443 位压缩加速

2013-09-27 15:14 246 查看
转化为01矩阵,将30行的每一列合并成一个int,对于某个询问进行与运算

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 111;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;

int g[40][10100];

int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
memset(g, 0, sizeof(g));
int p, cnt = 0, a, b, i, q, j, k;
p = 1;
for (i = 1; i <= n; ++i)
{
if (i % 30 == 0) {p = 1;cnt++;}
scanf("%d", &k);
for (j = 1; j <= k; ++j)
{
scanf("%d", &a);
g[cnt][a] |= p;
}
p <<= 1;
}
scanf("%d", &q);
for (i = 0; i < q; ++i)
{
scanf("%d%d", &a, &b);
int f = 0;
for (j = 0; j <= cnt; ++j)
{
if (g[j][a] & g[j][b]) break;
}
if (j != cnt + 1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: