[백준] 17471 게리맨더링
#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include <math.h>
#include <algorithm>
using namespace std;
int N;
vector<vector<int>> adj(11, vector<int>(11, 0));
vector<string> bitmasks;
void buildBitmasks(string bitmask) {
if (bitmask.length() == N) {
bitmasks.push_back(bitmask);
return;
}
buildBitmasks(bitmask + "0");
buildBitmasks(bitmask + "1");
}
bool bfs(int start, string bitmask) {
vector<int> visited(N, 0);
queue<int> q;
q.push(start);
while (!q.empty()) {
int cur = q.front();
q.pop();
if (visited[cur]) continue;
visited[cur] = 1;
for (int next = 0; next < N; ++next) {
if (!adj[cur][next]) continue;
if (bitmask[next] == '0') continue;
if (visited[next]) continue;
q.push(next);
}
}
for (int i = 0; i < N; ++i) {
if (visited[i]) bitmask[i] = '0';
}
if (stoi(bitmask) == 0) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N;
vector<int> people(11, 0);
for (int i = 0; i < N; ++i) {
int pp;
cin >> pp;
people[i] = pp;
}
for (int a = 0; a < N; ++a) {
int M;
cin >> M;
for (int j = 0; j < M; ++j) {
int b;
cin >> b;
b--;
adj[a][b] = 1;
adj[b][a] = 1;
}
}
buildBitmasks("");
int minDiff = 987654321;
for (int i = 0; i < bitmasks.size(); ++i) {
string area1 = bitmasks[i];
string area2 = "";
for (int j = 0; j < N; ++j) {
if (area1[j] == '0') area2 += "1";
else area2 += "0";
}
if (stoi(area1) == 0) continue;
if (stoi(area2) == 0) continue;
int start1 = -1;
int start2 = -1;
for (int j = 0; j < N; ++j) {
if (area1[j] == '1') start1 = j;
else start2 = j;
if ((start1 != -1) && (start2 != -1)) break;
}
if (!bfs(start1, area1)) continue;
if (!bfs(start2, area2)) continue;
int sum1 = 0;
int sum2 = 0;
for (int j = 0; j < N; ++j) {
if (area1[j] == '1') sum1 += people[j];
else sum2 += people[j];
}
minDiff = min(minDiff, abs(sum1 - sum2));
}
if (minDiff == 987654321) cout << -1;
else cout << minDiff;
return 0;
}