#include<stdio.h>
#define NODE_EXIST 1
typedef unsigned short ushort;
typedef unsigned int uint;
typedef /*struct*/ int TData;
/*{
uint id;
uint dat;
};*/
typedef TData * pData;
class Node;
typedef Node * pNode;
class Node
{
private:
TData dat;
uint fl;
pNode Old;
pNode Brt;
pNode Son;
public:
Node()
{
fl = 0;
Old = NULL;
Brt = NULL;
Son = NULL;
}
Node(TData nd, pNode pold)
{
fl = NODE_EXIST;
dat = nd;
Brt = NULL;
Son = NULL;
if (pold)
{
pNode cSon = pold->Son;
if (cSon)
{
while (cSon->Brt)
cSon = cSon->Brt;
cSon->Brt = this;
Old = cSon;
}
else
pold->Son = this;
}
}
~Node()
{
}
pNode operator[](uint sn)
{
pNode cSon = Son;
for (uint i=0; i<sn; i++)
{
cSon = cSon->Brt;
if (!cSon)
return NULL;
}
return cSon;
}
void SetData(TData a)
{
dat = a;
}
TData GetData()
{
return dat;
}
pNode GetOld()
{
return Old;
}
pNode GetBrt()
{
return Brt;
}
pNode GetSon()
{
return Son;
}
};
void Obhod(Node * n, int & s);
void Obhod(Node * n, int & s, int a);
void Obhod(Node * n, int & s)
{
int a = 0;
a++;
if (n->GetSon())
Obhod(n->GetSon(), s);
if (n->GetBrt())
Obhod(n->GetBrt(), s, a);
else
if(a == 2)
s++;
}
void Obhod(Node * n, int & s, int a)
{
a++;
if (n->GetSon())
Obhod(n->GetSon(), s);
if (n->GetBrt())
Obhod(n->GetBrt(), s, a);
else
if(a == 2)
s++;
}
void Zababac(Node * n, uint s)
{
if (s)
{
for (uint i=0; i<s; i++)
{
printf("%d\n", s*1000+i*10);
Zababac(new Node(s*1000+i*10, n), s-1);
}
}
}
void TreeFscanf(FILE * fin, Node * Tree)
{
int a, b, c, d;
fscanf(fin, "%d", &a);
Tree->SetData(a);
pNode RTree = NULL;
fscanf(fin, "%d", &a); //printf("a %d\n",a);
for(int t=1; t<=a; t++)
{
RTree = Tree;
fscanf(fin, "%d", &b); //printf("b %d\n",b);
for(int i=1; i<=b; i++)
{
if(i>1)
{
if(RTree->GetSon())
{
RTree = RTree->GetSon();
}
else
{
printf("input file error: Line in a file #%d -- 'The given father does not have sons.'\n", t+2);
return;
}
}
fscanf(fin, "%d", &c); //printf("c %d\n",c);
for(int j=1; j<c; j++)
{
if(RTree->GetBrt())
{
RTree = RTree->GetBrt();
}
else
{
printf("input file error: Line in a file #%d -- 'At the given father of only %d sons.'\n", t+2, j);
return;
}
}
}
fscanf(fin, "%d", &d); //printf("d %d\n",d);
new Node(d, RTree); //RTree = RTree->GetSon();
}
//Obhod(Tree); printf("\n\n");
}
int main()
{
int s=0;
pNode Tree = new Node();
FILE * fin;
fin = fopen("in.txt","r");
TreeFscanf(fin, Tree);
fclose(fin);
Obhod(Tree, s);
printf("%d\n", s);
return 0;
}