Cod sursa(job #659735)

Utilizator memaxMaxim Smith memax Data 10 ianuarie 2012 21:59:58
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
using namespace std;

struct tr{
          int k;
          tr * left, * right;
          } *t, *r = NULL;
int deep, id;
ofstream our ("scmax.out");

void ins(int z){
     tr * y, * x=r;
     int u=1;
     while (x!=NULL) {
           if(z==x->k) return;
           y=x;
           if(z<x->k){ x=x->left;
           }else{
                 x=x->right;
                 u++;
                 }
           }
     if(u>deep) {
                id = z;
                deep = u;
                }
     t=new tr;
     t->k=z;
     t->left=NULL;
     t->right=NULL;
     if(r==NULL){ r=t;} 
     else {
          if(z<y->k) { y->left=t; }
          else { y->right=t; }
          }
     }

void srch(int z){
     tr * y, * x=r;
     while (x->k!=z) {
           y=x;
           if(z<x->k){ x=x->left;
           }else{
                 x=x->right;
                 our << y->k << " ";
                 }
           }
     }

int main(){
    int a, n;
    ifstream inr ("scmax.in");
    inr >> n;
    for(int i=1; i<=n; i++){
             inr >> a;
             ins(a);
             }
    our << deep << "\n";
    srch(id);
    our << id;
    }