何等水题
某神犇仿关押罪犯的写法 却写挂了 然而实际上并不需要补集之类的
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 7 using namespace std; 8 9 const int Maxn=100010; 10 int n; 11 struct Data{ 12 int x,y; 13 Data(int x=0,int y=0):x(x),y(y) {} 14 }q[Maxn][2]; 15 int tot[2]; 16 int seq[Maxn*2]; 17 int hash(int x) { 18 return lower_bound(seq+1,seq+n*2+1,x) - seq; 19 } 20 int fa[Maxn*2]; 21 int find(int x) { 22 return fa[x]==x?x:fa[x]=find(fa[x]); 23 } 24 bool merge(int x,int y) { 25 x=find(x); 26 y=find(y); 27 if(x==y) return 0; 28 return fa[x]=y,1; 29 } 30 31 void init() { 32 tot[0]=tot[1]=0; 33 scanf("%d",&n); 34 for(int i=1;i<=n*2;i++) fa[i]=i; 35 int x,y,e; 36 for(int i=1;i<=n;i++) { 37 scanf("%d%d%d",&x,&y,&e); 38 seq[i*2-1]=x; 39 seq[i*2]=y; 40 q[++tot[e]][e] = Data(x,y); 41 } 42 sort(seq+1,seq+n*2+1); 43 } 44 45 bool check() { 46 for(int i=1;i<=tot[1];i++) { 47 merge(hash(q[i][1].x),hash(q[i][1].y)); 48 } 49 for(int i=1;i<=tot[0];i++) { 50 int x=hash(q[i][0].x),y=hash(q[i][0].y); 51 if(find(x)==find(y)) return 0; 52 } 53 return 1; 54 } 55 56 int main() { 57 freopen("prog.in","r",stdin); 58 freopen("prog.out","w",stdout); 59 60 int T; 61 for(scanf("%d",&T);T--;) { 62 init(); 63 if(check())puts("YES"); 64 else puts("NO"); 65 } 66 67 return 0; 68 }
时间: 2024-11-02 23:36:56