Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 73 Accepted Submission(s): 34
Problem Description
Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
Input
There are multiple input cases.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.
Output
For each test case, output the maximum number of remaining dominoes in a line.
Sample Input
2 3 0 0 0 3 0 1 1 1 1 3 4 5 0 1 0 2 3 1 2 2 0 0 1 0 2 0 4 1 3 2 0 0
Sample Output
4 6
Source
最大匹配可以做,不过比赛时用搜索过的。因为,这个题目比较特殊,图中只有两种图形,环,或者线,那么找到每个环和线的长度n,是奇数,就加n/2+1,偶数就是n/2.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
typedef long long LL;
const int maxn = 10000 + 5;
vector<int> G[maxn];
int vis[1000][1000];
int id[1000][1000];
struct Node{
int x,y;
Node(int x,int y){
this -> x = x;
this -> y = y;
}
};
vector<Node> v;
int cnt;
int ID(int x,int y){
if(id[x][y] == -1){
v.push_back(Node(x,y));
return id[x][y] = cnt++;
}
else return id[x][y];
}
int bfs(int x,int y){
int ret = 0;
int idd = ID(x,y);
queue<int> Q;
Q.push(idd);
vis[x][y] = 1;
while(!Q.empty()){
idd = Q.front();Q.pop();
//cout << v[idd].x << 'a' << v[idd].y << endl;
for(int i = 0;i < G[idd].size();i++){
int temx = v[G[idd][i]].x;
int temy = v[G[idd][i]].y;
if(vis[temx][temy] == 0){
Q.push(G[idd][i]);
ret++;
vis[temx][temy] = 1;
}
}
}
return ret;
}
int main(){
int n,m;
int x,y;
while(scanf("%d%d",&n,&m)){
if(n == 0 && m == 0) break;
v.clear();
cnt = 0;
for(int i = 0;i < maxn;i++) G[i].clear();
memset(vis,0,sizeof(vis));
memset(id,-1,sizeof(id));
for(int i = 0;i < n;i++){
scanf("%d%d",&x,&y);
G[ID(x,y)].push_back(ID(x+1,y));
G[ID(x+1,y)].push_back(ID(x,y));
}
for(int i = 0;i < m;i++){
scanf("%d%d",&x,&y);
G[ID(x,y)].push_back(ID(x,y+1));
G[ID(x,y+1)].push_back(ID(x,y));
}
int ans = 0;
for(int i = 0;i < v.size();i++){
int temx = v[i].x;
int temy = v[i].y;
if(vis[temx][temy] == 0){
int c = bfs(temx,temy);
if(c%2 == 1) ans += c/2+1;
else ans += c/2;
}
}
cout << ans << endl;
}
return 0;
}