forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapstrees.cpp
65 lines (53 loc) · 1.49 KB
/
heapstrees.cpp
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
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
bool cmp(multiset<int>& m1, multiset<int>& m2) {
return m1.size() > m2.size();
}
void dfs(vector<vector<int>>& adj, vector<int>& val, vector<multiset<int>>& sets, int curr) {
// Run DFS for all lower sets, keep track of largest
int biggest = curr;
for(int i = 0; i < adj[curr].size(); i++) {
int next = adj[curr][i];
dfs(adj, val, sets, next);
if(sets[next].size() > sets[biggest].size()) {
biggest = next;
}
}
// Set this node's set as the biggest child set
swap(sets[biggest], sets[curr]);
// For all child sets, merge into the parent's set
for(int i = 0; i < adj[curr].size(); i++) {
int next = adj[curr][i];
for(auto& j : sets[next]) {
sets[curr].insert(j);
}
}
// Find this val in our LIS
auto it = sets[curr].lower_bound(val[curr]);
// If too big, add to end
if(it == sets[curr].end()) {
sets[curr].insert(val[curr]);
}
// Otherwise, update a val
else {
sets[curr].erase(it);
sets[curr].insert(val[curr]);
}
}
int main() {
int n;
cin >> n;
vector<int> val(n);
vector<vector<int>> adj(n);
for(int i = 0; i < n; i++) {
cin >> val[i];
int par;
cin >> par;
if(par == 0) continue;
par--;
adj[par].push_back(i);
}
vector<multiset<int>> sets(n);
dfs(adj, val, sets, 0);
cout << sets[0].size() << endl;
}