From 33c53f5b04d46275394ba76f8108e458bc70fae2 Mon Sep 17 00:00:00 2001 From: sh0723 Date: Mon, 6 Jul 2026 17:19:19 +0900 Subject: [PATCH] =?UTF-8?q?17471=20:=20=EA=B2=8C=EB=A6=AC=EB=A7=A8?= =?UTF-8?q?=EB=8D=94=EB=A7=81=20resolve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week4/BOJ17471/BOJ17471_V2.cpp | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/week4/BOJ17471/BOJ17471_V2.cpp diff --git a/src/week4/BOJ17471/BOJ17471_V2.cpp b/src/week4/BOJ17471/BOJ17471_V2.cpp new file mode 100644 index 0000000..0cc3137 --- /dev/null +++ b/src/week4/BOJ17471/BOJ17471_V2.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +int N, ret = INT_MAX; +bool selected[11], visited[11]; +vector> inj; +int people[11]; +pair dfs(int here,bool group) { + visited[here] = true; + pair ret_val = {1, people[here]}; + for (int next : inj[here]) { + if (selected[next] != group || visited[next]) continue; + pair temp = dfs(next,group); + ret_val.first += temp.first; + ret_val.second += temp.second; + } + + return ret_val; +} +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N; + for (int i = 1; i <= N; i++) { + cin >> people[i]; + } + inj.resize(N+1); + for (int i = 1; i <= N; i++) { + int n; + cin >> n; + for (int j = 0; j < n; j++) { + int a; + cin >> a; + inj[i].push_back(a); + inj[a].push_back(i); + } + } + + for (int i = 1; i<(1 << N)-1; i++) { + memset(selected, false, sizeof(selected)); + memset(visited, false, sizeof(visited)); + int ast,bst; + for (int j=0; j group_a = dfs(ast, true); + pair group_b = dfs(bst, false); + + if (group_a.first + group_b.first == N) ret = min(ret, abs(group_a.second - group_b.second)); + } + + if (ret == INT_MAX) cout << -1 << '\n'; + else cout << ret << '\n'; + + return 0; +} \ No newline at end of file