-
H - Por Costel and the Match
- Gym - 100923H
-
#include<bits/stdc++.h> using namespace std; #define ll long long #define maxn 2000005 int pre[maxn]; int t,n,m,u,v; struct node { int pos,same; } x,y; node fond(int x) { node ans; if(pre[x]==0) { ans.same=1; ans.pos=x; return ans; } else if(pre[x]>0) { ans=fond(pre[x]); if(ans.same)pre[x]=ans.pos; else pre[x]=-ans.pos; return ans; } else { ans=fond(-pre[x]); if(ans.same)pre[x]=-ans.pos; else pre[x]=ans.pos; ans.same=!ans.same; return ans; } } int main() { freopen("meciul.in","r",stdin); freopen("meciul.out","w",stdout); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) pre[i]=0; while(m--) { scanf("%d%d",&u,&v); x=fond(u); y=fond(v); if(x.pos==y.pos) { if(x.same==y.same) printf("NO\n"); else printf("YES\n"); continue; } if(x.same==y.same) pre[x.pos]=-y.pos; else pre[x.pos]=y.pos; printf("YES\n"); } } return 0; }
H - Por Costel and the Match -种类并查集
最新推荐文章于 2022-05-13 00:19:39 发布
本文深入探讨了并查集算法的实现与应用,通过具体代码示例,详细讲解了如何使用并查集解决图论中节点连接性问题,特别是在处理大规模数据集时的效率优势。
157

被折叠的 条评论
为什么被折叠?



