标签:acm lse -- http space names name void https
并查集的学习:
https://blog.csdn.net/luomingjun12315/article/details/47373345
http://acm.hdu.edu.cn/showproblem.php?pid=1213
#include<iostream> using namespace std; int node[1050]; int deep[1050]; int sum; void Init(int n) { for(int i=1;i<=n;i++) { node[i]=i; deep[i]=0; } } int Find(int x) { if(x==node[x]) return x; return node[x]=Find(node[x]); } void Union(int x,int y) { x=Find(x); y=Find(y); if(x==y) return ; sum--; if(deep[x]<deep[y]) node[x]=y; else { node[y]=x; if(deep[x]==deep[y]) deep[x]++; } } int main() { int t; cin>>t; while(t--) { int n,m; cin>>n>>m; sum=n; Init(n); for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; Union(x,y); } cout<<sum<<endl; } return 0; }
标签:acm lse -- http space names name void https
原文地址:https://www.cnblogs.com/caijiaming/p/9102085.html