#include <iostream> #include <string> #include <vector>
using namespace std;
int maxValue = 0;
void dfs(int sheep, int woof, int start, int state, vector<int> info, vector<vector<int>>& grape, vector<vector<bool>>& check) { if (woof >= sheep) { return; }
maxValue = max(maxValue, sheep);
for (int i = 0; i < info.size(); i++) { int node = 1 << i; int isVisited = state & node; int nextState = state | node;
if (grape[start][i] == 0) { continue; }
if (check[nextState][i] == true) { continue; }
check[nextState][i] = true; if (isVisited) { dfs(sheep, woof, i, nextState, info, grape, check); } else { if (info[i] == 0) { dfs(sheep + 1, woof, i, nextState, info, grape, check); } else { dfs(sheep, woof + 1, i, nextState, info, grape, check); } } check[nextState][i] = false; } }
int solution(vector<int> info, vector<vector<int>> edges) { int answer = 0; vector<vector<int>> grape = vector<vector<int>>(info.size() + 1, vector<int>(info.size() + 1, 0)); vector<vector<bool>> check = vector<vector<bool>>((1 << 18) - 1, vector<bool>(17 + 1, false));
for (vector<int> edge : edges) { int from, to; from = edge[0]; to = edge[1];
grape[from][to] = 1; grape[to][from] = 1; }
maxValue = 0; int state = 1 << 0; check[state][0] = true;
dfs(1, 0, 0, state, info, grape, check);
answer = max(maxValue, answer);
return answer; }
|