-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollen.cpp
More file actions
70 lines (59 loc) · 1.85 KB
/
Copy pathpollen.cpp
File metadata and controls
70 lines (59 loc) · 1.85 KB
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
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
#define long long ll
#define array ar
#define stack st
bool adj[300][300] = {0}, stay[300] = {0}, visited[300] = {0};
int N, S, poln_pwr = -1;
pair<int, int> V[300];
void DFS(int u, int v, int bee_out = 0) {
// If the total amount of bees sent out is greater than the limit
if (bee_out > S) {
return;
}
visited[v] = true;
bool path = false;
for (int i = 0; i < N; i++) {
if (adj[v][i] && !visited[i]) {
path = true;
// If consecutive flowers are both absent
// The following family HAS to stay in
if (u != -1 && !stay[u]) {
DFS(v, i, bee_out);
} else {
// Else perform ordinary search
DFS(v, i, (!stay[v]) ? (bee_out + V[v].first) : bee_out);
stay[v] = !stay[v];
DFS(v, i, (!stay[v]) ? (bee_out + V[v].first) : bee_out);
}
}
}
if (!path) {
if (poln_pwr < bee_out) poln_pwr = bee_out;
}
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin >> N >> S;
for (int i = 0; i < N; i++) {
int s, p;
cin >> s >> p;
V[i] = {s, p};
stay[i] = true;
}
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u - 1][v - 1] = true;
adj[v - 1][u - 1] = true;
}
// Traverse the adjacency matrix, find the vine connection
// per vertex, start with node 0 to perform a complete search
// Start a BFS from src (0) and raise all propositions along the paths
// Conditions:
// - No more than two nodes consecutively can have the family absent
// - The total leave population canno be greater than S (upper limit)
DFS(-1, 0);
cout << poln_pwr << "\n";
return 0;
}