Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19-yuyu0830 #71

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
| 13차시| 2024.05.21 | LIS | [가장 긴 증가하는 부분 수열 2](https://www.acmicpc.net/problem/12015) | - |
| 15차시| 2024.06.01 | 기하 | [다각형의 면적](https://www.acmicpc.net/problem/2166) | - |
| 16차시 | 2024.06.04 | 별자리 만들기 | [별자리 만들기](https://www.acmicpc.net/problem/4386) | - |
| 19차시 | 2024.07.12 | 재귀 | [우수마을](https://www.acmicpc.net/problem/1949) | - |
---
55 changes: 55 additions & 0 deletions yuyu0830/재귀/1949.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>

#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#define NUM 10002

using namespace std;

// x : 이 노드를 선택하지 않았을 때의 최대값
// y : 이 노드를 선택했을 때의 최대값
struct node {
int x, y;
node(int X, int Y) : x(X), y(Y) {};
int m() { return max(x, y); }
};

bool visited[NUM] = {0, };
int arr[NUM] = {0, };
vector<int> v[NUM];

node f(int t) {
visited[t] = true;

node tmp(0, arr[t]);

// 이 노드에서 갈 수 있는 가지들을 탐색
for (auto i : v[t]) {
// 방문한 노드면 가지 않는다
if (!visited[i]) {
node a = f(i);

tmp.x += a.m();
tmp.y += a.x;
}
}

return tmp;
}

int main() {
fastio

int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];

for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}

printf("%d\n", f(1).m());
}