Advertisement
a53

Catalin_si_prietenii

a53
Jan 14th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <fstream>
  2. #define NMax 1000001
  3. using namespace std;
  4. ifstream f("prieteni.in");
  5. ofstream g("prieteni.out");
  6. int p,x,y,n,k;
  7. int root[NMax],dim[NMax];
  8.  
  9. int rad(int x)
  10. {
  11. while(root[x]!=x)
  12. x=root[x];
  13. return x;
  14. }
  15.  
  16. int main()
  17. {
  18. f>>n>>k;
  19. for(int i=1;i<=n;++i)
  20. {
  21. root[i]=i;
  22. dim[i]=1;
  23. }
  24. int vf1,vf2;
  25. for(int i=1;i<=k;++i)
  26. {
  27. f>>p;
  28. if(p==1)
  29. {
  30. f>>x>>y,vf1=rad(x),vf2=rad(y);
  31. if(vf1!=vf2)
  32. {
  33. if(dim[vf1]>dim[vf2])
  34. dim[vf1]+=dim[vf2],root[vf2]=vf1;
  35. else
  36. dim[vf2]+=dim[vf1],root[vf1]=vf2;
  37. }
  38. }
  39. else
  40. f>>x,g<<dim[rad(x)]<<'\n';
  41. }
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement