Question: The 1X2 bone card covers the plane. The input ensures that the horizontal bone cards (n) do not overlap, and the vertical bone cards (m) do not overlap, but the horizontal and vertical parts may overlap, ask how many dominoes do not interwork with each other after getting some dominoes (1 <= n, m <= 1000, the coordinate of the dominoes is (x, y) (0 <= x, y <= 100 )).
--> In the second multi-school case, WA5 hours is still WA, which is hard to understand...
Policy: Use and check the set. The two dominoes at the intersection are in the same set, but there cannot be loops. If a set has k elements, you can leave (k + 1)/2.
#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 100 + 10;const int maxv = 2000 + 10;vector<int> G[maxn][maxn];int f[maxv], w[maxv];void init(){ int i, j; for(i = 0; i < maxn; i++) for(j = 0; j < maxn; j++) G[i][j].clear(); for(i = 0; i < maxv; i++) f[i] = i; for(i = 0; i < maxv; i++) w[i] = 1;}int Find(int x){ return x == f[x] ? x : Find(f[x]);}void Union(int x, int y){ int newx = Find(x); int newy = Find(y); if(newx != newy) { f[newy] = newx; w[newx] += w[newy]; }}int main(){ int n, m, x, y, i, j; while(scanf("%d%d", &n, &m) == 2) { if(!n && !m) return 0; init(); for(i = 0; i < n; i++) { scanf("%d%d", &x, &y); G[y][x].push_back(i); G[y][x+1].push_back(i); } int N = n + m; for(i = n; i < N; i++) { scanf("%d%d", &x, &y); G[y][x].push_back(i); G[y+1][x].push_back(i); } for(i = 0; i <= 100; i++) for(j = 0; j <= 100; j++) if(G[i][j].size() == 2) { Union(G[i][j][0], G[i][j][1]);} int ret = 0; for(i = 0; i < N; i++) if(f[i] == i) ret += (w[i] + 1) / 2; printf("%d\n", ret); } return 0;}