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

ADD / week31 / 탁성건 #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
74 changes: 74 additions & 0 deletions 탁성건/week31/20303-할로윈의 양아치_baekjoon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct UnionFind {
vector<int> parent;
vector<int> cnt;
vector<int> candy;

UnionFind(int sz) :
parent(vector<int>(sz, -1)),
cnt(vector<int>(sz, 1)),
candy(vector<int>(sz)) {}

int Find(int x) {
if (parent[x] == -1) return x;
return parent[x] = Find(parent[x]);
}

void Union(int a, int b) {
int pa = Find(a);
int pb = Find(b);

if (pa == pb) return;

parent[pb] = pa;

cnt[pa] += cnt[pb];
cnt[pb] = 0;

candy[pa] += candy[pb];
candy[pb] = 0;
}
};

int main(void) {
cin.tie(nullptr)->sync_with_stdio(false);

int N, M, K;
cin >> N >> M >> K;

UnionFind uf(N);

for (auto& c : uf.candy)
cin >> c;

for (int i = 0; i < M; ++i) {
int u, v;
cin >> u >> v;
uf.Union(u - 1, v - 1);
}

vector<int> groupCnt;
vector<int> groupCandy;

for (int i = 0; i < N; ++i) {
if (uf.parent[i] != -1)
continue;

groupCnt.push_back(uf.cnt[i]);
groupCandy.push_back(uf.candy[i]);
}

vector<int> dp(K);

for (int idx = 0; idx < groupCnt.size(); ++idx)
for (int cnt = K - 1; cnt >= groupCnt[idx]; --cnt)
dp[cnt] = max(dp[cnt], dp[cnt - groupCnt[idx]] + groupCandy[idx]);

cout << dp[K - 1] << '\n';

return 0;
}