Code:
//this is the functor:
struct lvert
{
bool operator()(const vert &v1,const vert &v2)
{
//comparing the graph's verticles so they can be ordered.
if(v1.x==v2.x)
return v1.y<v2.y;
return v1.x<v2.x;
}
};
//gdb says that the problem comes from here:
int unite(vert v1,vert v2,map<vert,set<vert,lvert> ,lvert > &dsets,vector<vector<vert> >& verts)
{
cout << v1.x << ":" << v1.y << " " << v2.x << ":" << v2.y << endl;
set<vert,lvert> *S1 = &dsets[verts[v1.x][v1.y]]; //I guess this is where something is wrong
set<vert,lvert> *S2 = &dsets[verts[v2.x][v2.y]]; //or maybe here?
set<vert,lvert>::iterator i;
for(i=S2->begin(); i!=S2->end(); i++)
{
verts[i->x][i->y] = verts[v1.x][v1.y];
}
S1->insert(S2->begin(),S2->end());
dsets.erase(v2);
}
//and this code is somewhere in main() , it calls unite()
for(i=0; i<N; i++)
{
verts[i].resize(M);
for(j=0; j<M; j++)
{
verts[i][j] = vert(i,j);
dsets[vert(i,j)].insert(vert(i,j));
cin >> state;
vert v1(i,j);
vert v2(i+1,j);
vert v3(i,j+1);
if(state==3)
{
Sedges.erase(edge(i,j,i,j+1));
unite(v1,v3,dsets,verts);
Sedges.erase(edge(i,j,i+1,j));
unite(v1,v2,dsets,verts);
}
if(state==2)
{
Sedges.erase(edge(i,j,i,j+1));
unite(v1,v3,dsets,verts);
}
if(state==1)
{
Sedges.erase(edge(i,j,i+1,j));
unite(v1,v2,dsets,verts);
}
}
}
bar.