//UVA1607Gates
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int MAXN = 2e5 + 10;
int m, n;
struct Gate {
int a, b, t;
}Gates[MAXN];
int solve(int k) {//将前k位信号置为0
for(int i = 1; i <= m; i++) {
int a = Gates[i].a;
int b = Gates[i].b;
int va = a < 0 ? -a > k : Gates[a].t;
//当a小于0时,说明i的信号一部分来源于总输入信号,
//其中-a(正数)表示第-a位信号
//当a大于0时,说明i的信号来源于前面一个编号为a的门处理后生成的信号
//这段英文理解到吐血~~~~~,太晦涩
int vb = b < 0 ? -b > k : Gates[b].t;
Gates[i].t = !(va && vb);
//printf("Gates[%d].t = %d\n", i, Gates[i].t);
}
return Gates[m].t;
}
int Bsearch(int k) {
int L = 1, R = n;
while(L < R) {
int M = L + (R - L) / 2;
//printf("M = %d\n", M);
if(solve(M) == k) R = M;//说明M位后面的信号对最终信号不产生影响
else L = M + 1;//M位后面存在对本位产生影响的信号
}//二分,绝妙!!
return L;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d%d", &Gates[i].a, &Gates[i].b);
int v0 = solve(0);
int vn = solve(n);
//printf("v0 = %d, vn = %d\n", v0, vn);
if(v0 == vn) for(int i = 1; i <= n; i++) printf("0");
else {
int x = Bsearch(vn);
for(int i = 1; i <= x - 1; i++) printf("0");
printf("x");
for(int i = x + 1; i <= n; i++) printf("1");
}
printf("\n");
}
return 0;
}
/*
1
3 6
-1 -3 -1 -2 1 2 1 2 4 3 5 5
*/
UVA1607Gates
最新推荐文章于 2019-11-09 18:23:48 发布