Untitled
unknown
plain_text
a year ago
1.6 kB
9
Indexable
Never
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; class DisjointSet { public: vector<int>parent; vector<int>size; DisjointSet(int N) { parent.resize(N+1); size.resize(N+1,1); for(int i=0;i<=N;i++) parent[i]=i; } int fup(int x) { if(x==parent[x])return x; return parent[x]=fup(parent[x]); } void ubs(int u,int v) { int upu=fup(u); int upv=fup(v); if(upu==upv)return ; if(size[upu]<size[upv]) { parent[upu]=upv; size[upv]=size[upv]+size[upu]; } else { parent[upv]=upu; size[upu]=size[upu]+size[upv]; } } }; int main() { int n=5; int edges=4; vector<int>sol; vector<int>cof{2,2,1,1}; vector<int>cot{1,3,3,4}; vector<int>q{4,2,5}; DisjointSet ds(n); for(int i=0;i<edges;i++) { ds.ubs(cof[i],cot[i]); } for(int i=0;i<q.size();i++) { int par=ds.fup(q[i]); sol.push_back(ds.size[par]); } for(auto&x:sol)cout<<x<<" "; return 0; }