Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("flori2.in");
  8. ofstream fout("flori2.out");
  9.  
  10. #define nmax 355
  11.  
  12. int n,k,t[nmax+nmax],rg[nmax+nmax],x,nr,viz[nmax],m=0;
  13.  
  14. vector <int> v[nmax];
  15.  
  16. int Find(int nod)
  17. {
  18. while(nod!=t[nod])
  19. nod=t[nod];
  20. return nod;
  21. }
  22.  
  23. void Union(int x, int y)
  24. {
  25. if(rg[x]<rg[y])
  26. t[x]=y;
  27. if(rg[y]<rg[x])
  28. t[y]=x;
  29. if(rg[x]==rg[y])
  30. {
  31. t[x]=y;
  32. rg[y]++;
  33. }
  34. }
  35.  
  36. bool ok=0;
  37.  
  38. int main()
  39. {
  40. fin>>n>>k;
  41. for(int i=1;i<=nmax+nmax;i++)
  42. {
  43. t[i]=i;
  44. rg[i]=1;
  45. }
  46. for(int i=1;i<=n;i++)
  47. {
  48. ok=0;
  49. for(int j=1;j<=k;j++)
  50. {
  51. fin>>x;
  52. if(Find(x)==x)
  53. Union(x,i+nmax);
  54. else
  55. {
  56. if(Find(i+nmax)!=Find(x))
  57. {
  58. int nr1=Find(x)-nmax;
  59. int nr2=Find(i+nmax)-nmax;
  60. if(!viz[nr1] && !viz[nr2])
  61. {
  62. m++;
  63. viz[nr1]=m;
  64. viz[nr2]=m;
  65. }
  66. else
  67. {
  68. if(!viz[nr1] && viz[nr2])
  69. viz[nr1]=viz[nr2];
  70. else if(viz[nr1] && !viz[nr2])
  71. viz[nr2]=viz[nr1];
  72. }
  73. Union(Find(i+nmax),Find(x));
  74. }
  75. }
  76. }
  77. }
  78. for(int i=1;i<=n;i++)
  79. {
  80. if(viz[i])
  81. v[viz[i]].push_back(i);
  82. else
  83. {
  84. m++;
  85. v[m].push_back(i);
  86. }
  87. }
  88. for(int i=1;i<=m;i++)
  89. {
  90. vector <int> ::iterator it;
  91. for(it=v[i].begin();it!=v[i].end();it++)
  92. fout<<*it<<" ";
  93. fout<<'\n';
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement