-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_of_Iterations.cpp
More file actions
55 lines (44 loc) · 932 Bytes
/
Minimum_of_Iterations.cpp
File metadata and controls
55 lines (44 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class NAryTree{
int N;
list<int> *adj;
void getMinIterUtil(int v, int minIter[]);
public:
NAryTree(int N);
void addChild(int v, int w);
int getMinIter();
};
NAryTree::NAryTree(int N)
{
this->N=N;
adj=new List<int>[N];
}
void NAryTree::addChild(int v, int w)
{
adj[v].push_back(w);
}
void NAryTree::getMinIterUtil(int u, int minIter[])
{
minIter[u]=adj[u].size();
int maxChildItr=0;
int noOfMaxChildItr=0;
list<int>::iterator i;
for(i=adj[u].begin();i!=adj[u].end();++i)
{
getMinIterUtil(*i, minIter);
if(minIter[*i]>maxChildItr){
maxChildItr=minIter[*i];
noOfMaxChildItr=1;
}
else if(minIter[*i]==maxChildItr)
noOfMaxChildItr++;
}
minIter[u]=max(minIter[u], maxChildItr+noOfMaxChildItr);
}
int NAryTree::getMinIter()
{
int *minIter=new int[N];
for(int i=0;i<N;i++)
minIter[i]=0;
getMinIterUtil(0, minIter);
return minIter[0];
}