-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp3.cpp
More file actions
45 lines (38 loc) · 839 Bytes
/
Copy pathp3.cpp
File metadata and controls
45 lines (38 loc) · 839 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
#include<iostream>
#include<map>
using namespace std;
class BiTree{
public:
BiTree(): root(0) {}
friend istream& operator>>(istream& in, BiTree& tree){
tree.root=1;
int n; in>>n;
for(int i=1;i<=n;i++)
in>>tree.left[i]>>tree.right[i];
return in;
}
int weight(){
if(root==0) return 0;
map<int,int>d2c;
auto dfs=[&](auto&& self,int x,int d){
if(x==0) return;
++d2c[d];
self(self,left[x],d+1);
self(self,right[x],d+1);
};
dfs(dfs,root,0);
int res=0;
for(auto [d,c]:d2c)
res=max(res,c);
return res;
}
private:
int root;
map<int,int>left,right;
};
int main(){
BiTree tree;
cin>>tree;
cout<<tree.weight()<<endl;
return 0;
}