相等放并查集里,不等直接判断
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
vector<int> vx;
inline int mp(int x) {return upper_bound(vx.begin(),vx.end(),x)-vx.begin();}
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
const int N = 1e5+10;
//注意有n条指令至多有2*n个点
int f[N<<1];
int find(int u)
{
if(f[u] == u) return u;
return f[u] = find(f[u]);
}
void merge(int u,int v)
{
int f1 = find(u), f2 = find(v);
if(f1 != f2) f[f1] = f2;
}
void solve()
{
vx.clear();
int n;
cin>>n;
vector<array<int,3>> tmp(n);
vector<PII> pd;
for(int i=1;i<=2*n;++i) f[i] = i;
for(int i=0;i<n;++i)
{
int u,v,e;
cin>>u>>v>>e;
tmp[i] = {u,v,e};
vx.push_back({u});
vx.push_back({v});
}
sort(vx.begin(),vx.end());
for(int i=0;i<n;++i)
{
auto [u,v,e] = tmp[i];
if(e) merge(mp(u),mp(v));
else pd.push_back({mp(u),mp(v)});
}
bool ok = true;
for(auto [s,t]:pd)
{
if(find(s) == find(t)) ok = false;
}
cout<<(ok?("YES\n"):("NO\n"));
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
cin>>T;
while(T--)
{
solve();
}
}
标签:P1955,typedef,return,int,查集,long,NOI2015,vector,vx
From: https://www.cnblogs.com/ruoye123456/p/18438894