Poj-2524-Ubiquitous (Religions)
There are n students in a school, m of which share the same religious beliefs,
Ask the maximum number of religions in this school.
Solution: basic and query sets. People with the same faith are classified as a collection,
The number of the last set is the result. Evaluate the number of sets: determine the number of root nodes and use the characteristics of the root node (the parent node is its own)
The Code is as follows:
#include
#include
#include
using namespace std;int n,m;int f[52014];void init(){ for(int i=1; i<=n; i++) f[i]=i;}int find(int x){ return f[x]==x?x:find(f[x]);}void unity(int a,int b){ if(find(a)!=find(b)) f[find(a)]=find(b);}int main(){ int ji=1; while(cin>>n>>m) { if(n+m==0)break; init(); while(m--) { int a,b; cin>>a>>b; unity(a,b); } int ans=0; for(int i=1; i<=n; i++) if(f[i]==i) ans++; cout<<"Case "<