BZOJ4195 NOI2015 程序自动分析

4195: [Noi2015]程序自动分析

Time Limit: 10 Sec  Memory Limit: 512 MB

Description
在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足。
考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量相等/不等的约束条件,请判定是否可以分别为每一个变量赋予恰当的值,使得上述所有约束条件同时被满足。例如,一个问题中的约束条件为:x1=x2,x2=x3,x3=x4,x1≠x4,这些约束条件显然是不可能同时被满足的,因此这个问题应判定为不可被满足。
现在给出一些约束满足问题,请分别对它们进行判定。 Input
输入文件的第1行包含1个正整数t,表示需要判定的问题个数。注意这些问题之间是相互独立的。
对于每个问题,包含若干行:
第1行包含1个正整数n,表示该问题中需要被满足的约束条件个数。
接下来n行,每行包括3个整数i,j,e,描述1个相等/不等的约束条件,相邻整数之间用单个空格隔开。若e=1,则该约束条件为xi=xj;若e=0,则该约束条件为xi≠xj。 Output
输出文件包括t行。
输出文件的第k行输出一个字符串“YES”或者“NO”(不包含引号,字母全部大写),“YES”表示输入中的第k个问题判定为可以被满足,“NO”表示不可被满足。 Sample Input
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1 Sample Output
NO
YES HINT
在第一个问题中,约束条件为:x1=x2,x1≠x2。这两个约束条件互相矛盾,因此不可被同时满足。
在第二个问题中,约束条件为:x1=x2,x2=x1。这两个约束条件是等价的,可以被同时满足。
1≤n≤1000000
1≤i,j≤1000000000

算法讨论:

并查集。我用了两个,一个来维护不等,一个来维护相同,如果有特别可恶的非法,那么在加入中途就可以判Fifa。

然后我们对最后的结果Check,如果两个点既在两个并查集中都是在一个集合中,那么一定是Fifa的。

然后你知道我被并查集卡爆栈了吗?

我从前的写法都是这样

fa[find(x)] = find(y)

但是我告诉你,以后一定要这样写: fa[find(y)] = find(x)

否则会爆栈。。。。点数特别多,而且是个链。。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cassert> using namespace std;
inline long long read() {
long long x = 0; char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
} const int N = 100000 + 5;
int n, tail, ks;
int que[N << 1], f1[N << 1], f2[N << 1]; struct Query {
long long x, y;
int type;
}q[N]; void Input() {
n = read(); tail = 0;
for(int i = 1; i <= n; ++ i) {
q[i].x = read(); q[i].y = read(); q[i].type = read();
que[++ tail] = q[i].x;
que[++ tail] = q[i].y;
}
sort(que + 1, que + tail + 1);
tail = unique(que + 1, que + tail + 1) - que - 1;
assert(tail > 0);
for(int i = 1; i <= n; ++ i) {
q[i].x = lower_bound(que + 1, que + tail + 1, q[i].x) - que;
q[i].y = lower_bound(que + 1, que + tail + 1, q[i].y) - que;
}
} int find(int *f, int x) {
return f[x] == x ? x : (f[x] = find(f, f[x]));
}
//f1 if a == b we u(a, b)
//f2 if a != b we u(a, b) void Solve() {
int up = tail, fx, fy;
for(int i = 1; i <= up; ++ i) f1[i] = i, f2[i] = i;
for(int i = 1; i <= n; ++ i) { if(q[i].x == q[i].y)
if(!q[i].type) { puts("NO"); return; }
else continue;
if(q[i].type) {
fx = find(f2, q[i].x), fy = find(f2, q[i].y);
if(fx == fy) { puts("NO"); return; }
f1[find(f1, q[i].y)] = find(f1, q[i].x);
}
else {
fx = find(f1, q[i].x), fy = find(f1, q[i].y);
if(fx == fy) { puts("NO"); return; }
f2[find(f2, q[i].y)] = find(f2, q[i].x);
}
}
for(int i = 1; i <= n; ++ i) {
if(q[i].x == q[i].y) continue;
if(find(f1, q[i].x) == find(f1, q[i].y) && find(f2, q[i].x) == find(f2, q[i].y)) {
puts("NO"); return;
}
}
puts("YES");
return;
} #define stone_ int main() {
#ifndef stone_
freopen("prog.in", "r", stdin);
freopen("prog.out", "w", stdout);
#endif int T;
scanf("%d", &T);
while(T --) {
Input();ks++;
Solve();
} #ifndef stone_
fclose(stdin); fclose(stdout);
#endif
return 0;
}
上一篇:【洛谷P3919】可持久化数组


下一篇:[NOI2015]程序自动分析(并查集,离散化)