Advertisement
Guest User

Untitled

a guest
Apr 15th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <map>
  9. #include <algorithm>
  10. #include <fstream>
  11. #include <queue>
  12. #include <memory.h>
  13. #include <stack>
  14. #include <deque>
  15.  
  16. #define mp make_pair
  17. #define pb push_back
  18.  
  19. #define taskname "A"
  20.  
  21. #ifdef LOCAL
  22.   #define LLD "%I64d"
  23. #else
  24.   #define LLD "%lld"
  25. #endif
  26.  
  27. using namespace std;
  28.  
  29. typedef long long int64;
  30. typedef long double ld;
  31.  
  32. int counter,n,m,vertex;
  33.  
  34. void root(int a,int b){
  35.   printf("0 %d %d\n",a+1,b+1);
  36.   ++counter;
  37.   if (counter==m)
  38.     exit(0);
  39. }
  40.  
  41. void uni(int a,int b){
  42.   printf("1 %d %d\n",a+1,b+1);
  43.   ++counter;
  44.   if (counter==m)
  45.     exit(0);
  46. }
  47.          
  48. struct tree{
  49.   tree *T1,*T2;
  50.   int root;
  51.   tree(){
  52.     root=-1;
  53.     T1=T2=0;    
  54.   }
  55.   tree(tree* t1,tree* t2){
  56.     assert(t1 && t2);
  57.     T1=t1;
  58.     T2=t2;
  59.     root=t1->root;
  60.   }
  61. };
  62.  
  63. tree* getnew(){
  64.   tree* tmp=new tree;
  65.   tmp->root=vertex++;
  66.   return tmp;
  67. }        
  68.  
  69. int deep(tree* tr){
  70.   if (tr->T2)
  71.     return deep(tr->T2);
  72.   return tr->root;
  73. }
  74.  
  75. int root(tree* tr){
  76.   if (tr->T1)
  77.     return root(tr->T1);
  78.   return tr->root;
  79. }
  80.  
  81. tree* dop(tree* tr,tree* ne){
  82.   if (tr->T1){
  83.     swap(tr->T1,tr->T2);
  84.     tr->T1=dop(tr->T1,ne);
  85.     return tr;
  86.   }
  87.   tree* tmp=new tree();
  88.   tmp->T1=ne;
  89.   tmp->T2=tr;
  90.   return tmp;
  91. }
  92.  
  93. void iterate(tree* &tr){
  94.   tree* tmp=getnew();
  95.   uni(tmp->root,root(tr));
  96.   int dp=deep(tr);
  97.   root(dp,dp);
  98.   tr=dop(tr,tmp);
  99. }
  100.  
  101. void print(tree* tr){
  102.   if (!tr->T1){
  103.     cerr<<tr->root<<endl;
  104.     return;
  105.   }
  106.   cerr<<root(tr)<<" "<<root(tr->T1)<<" "<<root(tr->T2)<<endl;
  107.   print(tr->T1);
  108.   print(tr->T2);
  109. }
  110.  
  111. int main(){
  112. #ifdef LOCAL
  113.   freopen(taskname".in","r",stdin);
  114.   freopen(taskname".out","w",stdout);
  115. #endif
  116.   scanf("%d %d",&n,&m);
  117.   tree* tmp=getnew();
  118.   for (;;){
  119.     iterate(tmp);
  120.  //   print(tmp);
  121. //    cerr<<endl;
  122.   }
  123.   return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement