[Problem solution] grid access && knight coexistence problem (the largest independent set of bipartite graphs)

Grid access problem portal

Cavaliers coexistence problem portal

analysis

The two questions are put together because they have a lot in common.

Just look at the background of the title and you will know that they are actually quite similar: they are all on a chessboard (grid), and there is a mutually exclusive relationship between points. In fact, their solution ideas are almost exactly the same.

Grid access

Let me talk about the number of squares.

First of all, we find the most important feature of this type of problem: points are mutually exclusive, and each point can only be selected and not selected. So we can consider constructing a bipartite graph and establishing a super source point s ​ ss and sinkt tT , and finally Minimal Cut (maximum flow). The specific establishment process is as follows:

  1. First, dye the mesh in black and white. For example, if the sum of the abscissas and ordinates are odd numbers, they are dyed black, and the even numbers are dyed white, so adjacent black and white points cannot be selected at the same time. This step does not need to be really colored with code, just judge the sum of the horizontal and vertical coordinates when building the map, which is also convenient for later understanding.
  2. So we consider putting all the black points on the left side of the bipartite graph, if a black point and ssIf s is connected, it means it is selected. In the same way, put the white dot on the right, if it is the same asttIf t is connected, it means that the white dot is selected. Considering the weight of the edge, obviously if a point (no matter black and white) is selected, the value is the point weight of the current point, so the black and white point issss andttThe edge weight of t even equals the point weight.
  3. Then consider the constraints. Obviously, if we want to choose at most one of the two points, then we must make one of the points to sss orttThe connecting edge of t is cut off when seeking the minimum cut (you can review the definition in 2). Therefore, for each black dot, connect the white dots affected by it asINF INFThe edge of I N F , due to the nature of the minimum cut, the edge weight isINF INFThe edges of I N F cannot be cut off.
  4. Run the minimum cut (maximum flow) template, and the final answer is the point weight sum minus the minimum cut.

Code (map)

Show You the Code.

for(int i = 1; i <= m; i++){
    
    
    for(int j = 1; j <= n; j++){
    
    
        scanf("%d", &a[i][j]);
        sum += a[i][j];
        if((i+j)&1){
    
    		//判断黑白点
            add(s, get(i, j), a[i][j]);
            add(get(i, j), s, 0);
        }
        else{
    
    
            add(get(i, j), t, a[i][j]);
            add(t, get(i, j), 0);
        }
    }
}
for(int i = 1; i <= m; i++){
    
    
    for(int j = 1; j <= n; j++){
    
    
        for(int k = 0; k < 4; k++){
    
    		//四个方向都会影响到
            if(!((i+j)&1)) continue;
            int u = i+mx[k], v = j+my[k];
            if(u<=0 || u>m || v<=0 || v>n) continue;
            add(get(i, j), get(u, v), INF);	//get(x,y)把二维坐标转化为一位编号. get(x,y)=(n-1)*x+y
            add(get(u, v), get(i, j), 0);
        }
    }
}
dinic();
cout << sum-ans << endl;	//ans是最大流

Small summary

Let's go back and see what the essential operation is after we build the map in this problem.

We consider the graph that was finally cut in half. The points in the graph that affect each other must no longer be in the same set, and when we build the graph, we build edges when they have an impact, so we actually find out from the initial bipartite graph The largest independent set (that is, the largest set of points in which there is no connection between two points).


Cavaliers coexist

(After finishing the previous question, Metropolis Knights should coexist)

Similarly, we found that although the way of influencing has changed, if we continue to dye in the same way, the black and white points will still only affect each other, and will not directly affect the points of the same color (see the picture in the title). This shows that we can actually mess around in the same way.

This question has many obstacles, but it is actually useless. When encountering obstacles, just skip the unconnected edges.

Code (still drawing)

for (int i = 1; i <= n; ++i) {
    
    
    for (int j = 1; j <= n; ++j) {
    
    
        if(mp[i][j]) continue;		//mp[i][j]为1则表示有障碍
        if((i+j)&1){
    
    
            add(s, get(i,j), 1);		//点权实际可以看做1
            add(get(i,j), s, 0);
        }
        else{
    
    
            add(get(i,j), t, 1);
            add(t, get(i,j), 0);
        }
    }
}
for (int i = 1; i <= n; ++i) {
    
    
    for (int j = 1; j <= n; ++j) {
    
    
        if(mp[i][j] || !((i+j)&1)) continue;
        int u, v;
        for (int k = 0; k < 8; ++k) {
    
    
            u = i+mx[k], v = j+my[k];
            if(u<=0 || u>n || v<=0 || v>n || mp[u][v]) continue;
            add(get(i, j), get(u, v), INF);
            add(get(u, v), get(i, j), 0);
        }
    }
}
dinic();
cout << n*n-m-ans << endl;		//总共n*n个点,去掉m个障碍,再减去最大流

Real summary

We can find that in a grid model like this, we only need to build a map according to the intention of the question, and then find the largest independent set. (It seems shorter than the summary)

Guess you like

Origin blog.csdn.net/qq_30115697/article/details/89575910