Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f9b38b

Browse files
Create LIS On Tree.cpp
1 parent 0ba5fb4 commit 0f9b38b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <stack>
4+
5+
using namespace std;
6+
7+
const int MAX_N = 2e5 + 5;
8+
9+
vector <stack <int> > minimum_ending_of_length(MAX_N);
10+
vector <int> A(MAX_N);
11+
vector <vector <int> > tree(MAX_N);
12+
vector <int> LIS_here(MAX_N);
13+
14+
int find_first_greater(int n)
15+
{
16+
int left = 0, right = MAX_N - 1;
17+
18+
//L < x <= R
19+
while(right - left > 1)
20+
{
21+
int mid = (left + right)/2;
22+
23+
if(minimum_ending_of_length[mid].size() == 0 || minimum_ending_of_length[mid].top() >= n)
24+
{
25+
right = mid;
26+
}
27+
else
28+
{
29+
left = mid;
30+
}
31+
}
32+
33+
return right;
34+
}
35+
36+
int find_LIS()
37+
{
38+
int left = 0, right = MAX_N - 1;
39+
40+
//L <= x < R
41+
while(right - left > 1)
42+
{
43+
int mid = (left + right)/2;
44+
45+
if(minimum_ending_of_length[mid].size() > 0)
46+
{
47+
left = mid;
48+
}
49+
else
50+
{
51+
right = mid;
52+
}
53+
}
54+
55+
return left;
56+
}
57+
58+
void dfs(int v, int parent_v)
59+
{
60+
int best_position = find_first_greater(A[v]);
61+
minimum_ending_of_length[best_position].push(A[v]);
62+
63+
LIS_here[v] = find_LIS();
64+
65+
for(int child_v : tree[v])
66+
{
67+
if(child_v == parent_v)
68+
{
69+
continue;
70+
}
71+
72+
dfs(child_v, v);
73+
}
74+
75+
minimum_ending_of_length[best_position].pop();
76+
}
77+
78+
int main()
79+
{
80+
int no_of_vertices;
81+
cin >> no_of_vertices;
82+
83+
for(int i = 1; i <= no_of_vertices; i++)
84+
{
85+
cin >> A[i];
86+
}
87+
88+
int no_of_edges = no_of_vertices - 1;
89+
for(int i = 1; i <= no_of_edges; i++)
90+
{
91+
int u, v;
92+
cin >> u >> v;
93+
94+
tree[u].push_back(v);
95+
tree[v].push_back(u);
96+
}
97+
98+
dfs(1, 0);
99+
100+
for(int v = 1; v <= no_of_vertices; v++)
101+
{
102+
cout << LIS_here[v] << "\n";
103+
}
104+
105+
return 0;
106+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /