Advertisement
Guest User

Untitled

a guest
Nov 11th, 2012
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include<fstream>
  2. #define nmax 5000
  3. #define comanda 1
  4. using namespace std;
  5. struct nod
  6. {int elem,cheie;
  7.      nod *st,*dr;};
  8.  
  9. struct operatie{
  10.     int x,y,tip;};
  11.  
  12. int tata[nmax+1],h[nmax+1],rasp[nmax];
  13. int N,nrk,nrrasp;
  14.  
  15. nod *v[nmax];
  16. operatie op[nmax+1];
  17.  
  18. //Proceduri arbori
  19. void adauga(nod* &rad,int elem,int cheie)
  20. {
  21.     if(rad==NULL)
  22.      {
  23.          rad=new nod;
  24.          rad->elem=elem;
  25.          rad->cheie=cheie;
  26.          rad->st=rad->dr=NULL;
  27.      }
  28.      else
  29.      {
  30.          if(elem<rad->elem)
  31.            adauga(rad->st,elem,cheie);
  32.            else
  33.            adauga(rad->dr,elem,cheie);
  34.      }
  35. }
  36.  
  37. int det_cheie(nod* rad,int elem)
  38. {
  39.     while(rad!=NULL)
  40.     {
  41.         if(rad->elem==elem)
  42.           return rad->cheie;
  43.  
  44.           if(elem<rad->elem)
  45.           rad=rad->st;
  46.           else
  47.           rad=rad->dr;
  48.     }
  49.     return 0;
  50. }
  51. void eliberare(nod* &rad)
  52. {
  53.     if(rad!=NULL)
  54.      {
  55.          if(rad->st)
  56.           eliberare(rad->st);
  57.           if(rad->dr)
  58.           eliberare(rad->dr);
  59.  
  60.          delete rad;
  61.      }
  62. }
  63.  
  64. //Proceduri multimi
  65. int reprezentant(int elem);
  66. int reprezentant_cheie(int cheie);
  67.  
  68. void unifica(int elem1,int elem2)
  69. {
  70.     int x,y;
  71.     x=reprezentant_cheie(det_cheie(v[elem1%nmax],elem1));
  72.     y=reprezentant_cheie(det_cheie(v[elem2%nmax],elem2));
  73.  
  74.     if(h[x]>h[y])
  75.     {
  76.         tata[y]=x;
  77.     }
  78.     else
  79.     {
  80.         tata[x]=y;
  81.         if(h[x]==h[y])
  82.          h[y]++;
  83.     }
  84. }
  85.  
  86. int reprezentant(int elem)
  87. {
  88.     int x,y,t,r;
  89.  
  90.     x=det_cheie(v[elem%nmax],elem);
  91.     r=x;
  92.  
  93.     while(tata[r])
  94.     r=tata[r];
  95.  
  96.     y=x;
  97.     while(y!=r)
  98.     {
  99.         t=tata[y];
  100.         tata[y]=r;
  101.         y=t;
  102.     }
  103.  
  104.     return r;
  105. }
  106.  
  107. int reprezentant_cheie(int cheie)
  108. {
  109.     int x,y,t,r;
  110.  
  111.     x=cheie;
  112.     r=x;
  113.  
  114.     while(tata[r])
  115.     r=tata[r];
  116.  
  117.     y=x;
  118.     while(y!=r)
  119.     {
  120.         t=tata[y];
  121.         tata[y]=r;
  122.         y=t;
  123.     }
  124.  
  125.     return r;
  126. }
  127.  
  128. void init_arbori()
  129. {
  130.     int i,x,y;
  131.     for(i=1;i<=N;i++)
  132.     {
  133.         if((det_cheie(v[op[i].x%nmax],op[i].x))== 0)
  134.          {nrk++;
  135.            adauga(v[op[i].x%nmax],op[i].x,nrk);
  136.            }
  137.          if((det_cheie(v[op[i].y%nmax],op[i].y))==0)
  138.          {
  139.              nrk++;
  140.              adauga(v[op[i].y%nmax],op[i].y,nrk);
  141.          }
  142.     }
  143. }
  144. void rezolva()
  145. {
  146.     int i,r1,r2;
  147.  
  148.     init_arbori();
  149.  
  150.     for(i=1;i<=N;i++)
  151.     {
  152.         if(op[i].tip==comanda)
  153.          unifica(op[i].x,op[i].y);
  154.          else
  155.          {
  156.              nrrasp++;
  157.              r1=reprezentant(op[i].x);
  158.              r2=reprezentant(op[i].y);
  159.              if(r1==r2)
  160.              rasp[nrrasp]=1;
  161.              else
  162.              rasp[nrrasp]=0;
  163.          }
  164.     }
  165.  
  166.     for(i=0;i<N;i++)
  167.     eliberare(v[i]);
  168. }
  169. void citeste()
  170. {
  171.     int i;
  172.     ifstream f("entries.in");
  173.     f>>N;
  174.     for(i=1;i<=N;i++)
  175.     f>>op[i].x>>op[i].y>>op[i].tip;
  176.  
  177.     f.close();
  178. }
  179. void scrie()
  180. {
  181.     int i;
  182.     ofstream g("entries.out");
  183.  
  184.     for(i=1;i<=nrrasp;i++)
  185.     g<<rasp[i]<<'\n';
  186.  
  187.     g.close();
  188. }
  189.  
  190. int main()
  191. {
  192.     citeste();
  193.     rezolva();
  194.     scrie();
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement