代码(样例已过,交上去WA):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <limits.h>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f, INF_BIT = 0x3f;
const int N = 2e6 + 10;
int t;
int n;
int x, y, opt;
struct Node{
int x, y, opt;
};
vector<Node> add;
vector<int> alls;
int find(int x){
int l = 0, r = alls.size() - 1;
while(l <= r){
int mid = l + (r - l) / 2;
if(x <= alls[mid]) r = mid - 1;
else l = mid + 1;
}
return r;
}
int fa[N];
int dis[N];
int get(int x){
if(fa[x] == x) return x;
else{
int t = get(fa[x]);
dis[x] += dis[fa[x]];
return fa[x] = t;
}
}
int main(){
cin >> t;
while(t--){
add.clear();
alls.clear();
memset(dis, 0, sizeof(dis));
cin >> n;
for(int i = 1;i <= n;i++){
cin >> x >> y >> opt;
add.push_back({x, y, opt});
alls.push_back(x);
alls.push_back(y);
}
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(), alls.end()),
alls.end());
for(int i = 1;i <= alls.size();i++){
fa[i] = i;
}
bool flag = true;
for(int i = 1;i <= n;i++){
int x = find(add[i].x), y = find(add[i].y),
opt = add[i].opt;
if(opt == 0 && add[i].x == add[i].y){
flag = false;
break;
}
int fx = get(x), fy = get(y);
if(opt == 1){
if(fx == fy && (dis[x] - dis[y]) % 2){
flag = false;
break;
}else{
fa[fx] = fy;
dis[fx] = dis[y] - dis[x];
}
}else if(opt == 0){
if(fx == fy && (dis[x] - dis[y] - 1) % 2){
flag = false;
break;
}else{
fa[fx] = fy;
dis[fx] = dis[y] + 1 - dis[x];
}
}
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}
而且我还把第一个数据点下下来:
5
2
1 2 1
1 2 0
3
1 2 1
2 3 1
3 1 1
4
1 2 1
2 3 1
3 4 1
1 4 0
2
1 2 1
2 1 1
1
1 1 1
NO
YES
NO
YES
YES
本地输出正确,但洛谷IDE(linux)上输出错误:
NO
NO
NO
YES
YES
求助:为什么?