-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar_road.cpp
65 lines (50 loc) · 1.49 KB
/
star_road.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 <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 10000; // Assume an upper limit for N
const int MAX_EDGES = 2 * MAX_N; // Since each edge is stored twice in adjacency list
int estrelas[MAX_N];
int adj[MAX_EDGES];
int head[MAX_N + 1], next_adj[MAX_EDGES];
bool visitado[MAX_N + 1];
int max_count = 0;
void add_edge(int u, int v, int& edge_count) {
adj[edge_count] = v;
next_adj[edge_count] = head[u];
head[u] = edge_count++;
}
void dfs(int cidade, int ultima_estrela, int msc_atual) {
max_count = max(max_count, msc_atual);
visitado[cidade] = true;
for (int i = head[cidade]; i != -1; i = next_adj[i]) {
int vizinha = adj[i];
if (!visitado[vizinha]) {
dfs(vizinha, ultima_estrela, msc_atual);
if (estrelas[vizinha - 1] > ultima_estrela) {
dfs(vizinha, estrelas[vizinha - 1], msc_atual + 1);
}
}
}
visitado[cidade] = false;
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> estrelas[i];
}
fill(head, head + N + 1, -1); // Initialize head array
int edge_count = 0;
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
add_edge(u, v, edge_count);
add_edge(v, u, edge_count);
}
for (int inicio = 1; inicio <= N; ++inicio) {
fill(visitado, visitado + N + 1, false);
dfs(inicio, estrelas[inicio - 1], 1);
}
cout << max_count << endl;
return 0;
}