# 886. Possible Bipartition 问题 求问代码错在哪里

runtime error Line 346: Char 35: runtime error: member access within misaligned address 0x000300000001 for type ‘struct __node_type’, which requires 8 byte alignment (hashtable_policy.h)

``````class Solution {
public:
bool isValid(unordered_set<int> group, vector<vector<int>>& dislikes){
for(int i = 0; i< dislikes.size(); i++){
int a = dislikes[i][0];
int b = dislikes[i][1];
if(group.count(a)&&group.count(b))
return false;
}
return true;
}

bool dfs(unordered_set<int>& one, unordered_set<int>& two, vector<vector<int>>& dislikes){
for(auto it = two.begin(); it!=two.end(); it++){
bool a = false;

one.insert(*it);
two.erase(*it);
if(isValid(one, dislikes)&&isValid(two, dislikes)){
return true;
}else if(isValid(one, dislikes)&&!isValid(two, dislikes)){
a = dfs(one, two, dislikes);
}else {
one.erase(*it);
two.insert(*it);
}
if(a){
return true;
}
}
return false;
}
bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
unordered_set<int> one, two;
for(int i = 1; i<=N; i++){
two.insert(i);
}
bool res = dfs(one, two, dislikes);
return res;
}
};
``````

https://paste.ubuntu.com/p/WtSv9tPhqD/

c++的unordered_set底层是用hash map实现的 我没有用hash map

set 改成 long试试？

iterator遍历的时候是不能修改的

``````class Solution {
public:
bool isValid(unordered_map<int,vector<int>>& graph, int color, int cur, vector<int>& colors){
if(colors[cur]!= 0){
return colors[cur] == color;
}

colors[cur] = color;
for(int node:graph[cur]){
if(!isValid(graph, -1*color, node, colors))
return false;
}
return true;
}

bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
vector<int> colors(N+1,0);
unordered_map<int,vector<int>> graph;
for(int i = 0; i<dislikes.size(); i++){
int a = dislikes[i][0];
int b = dislikes[i][1];
graph[a].push_back(b);
graph[b].push_back(a);

}
for(int i = 1; i<=N ; i++){
if(colors[i] == 0&&!isValid(graph,1,i,colors))
return false;
}
return true;
}
};
``````