您的位置:首页 > 其它

hdu1507Uncle Tom's Inherited Land*

2016-02-19 21:11 477 查看


Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2864    Accepted Submission(s): 1181
Special Judge


Problem Description

Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of
the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the
size of two squares of your uncle's property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks). 



 

Input

Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of
squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.

 

Output

For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more
than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.

 

Sample Input

4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
4 3
4
4 2
3 2
2 2
3 1
0 0

 

Sample Output

4
(1,2)--(1,3)
(2,1)--(3,1)
(2,3)--(3,3)
(2,4)--(3,4)

3
(1,1)--(2,1)
(1,2)--(1,3)
(2,3)--(3,3)

 

Source

South America 2002 - Practice

奇偶二分图

#include<bits/stdc++.h>
#include<algorithm>
const int MAXN=10010;
const int MAXM=20010;
struct Edge{
int to,next;
}edge[MAXM];
int tot;
int mp[111][111];
bool used[MAXN];
int linker[MAXN];
int head[MAXN];

void init(){
tot=0;
memset(head,-1,sizeof(head));
}

void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}

bool dfs(int u){
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(!used[v]){
used[v]=true;
if(linker[v]==-1||dfs(linker[v])){
linker[v]=u;
return true;
}
}
}
return false;
}
int m;

int hungary(int n){
memset(linker,-1,sizeof(linker));
int ret=0;
for(int u=1;u<=n;u++){
if(((u%m==0? m:u%m)+(u-1)/m+1)%2==1){
memset(used,false,sizeof(used));
if(dfs(u))
ret++;
}
}
return ret;
}

int main(){
int n,k;
int u,v;
int case1=1;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)
break;
if(case1!=1)
printf("\n");
case1++;
init();
scanf("%d",&k);
memset(mp,0,sizeof(mp));
while(k--){
scanf("%d%d",&u,&v);
mp[u][v]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if((i+j)%2==1&&mp[i][j]==0){
if(i-1>=1&&mp[i-1][j]==0)
addedge((i-1)*m+j,(i-2)*m+j);
if(j-1>=1&&mp[i][j-1]==0)
addedge((i-1)*m+j,(i-1)*m+j-1);
if(i+1<=n&&mp[i+1][j]==0)
addedge((i-1)*m+j,i*m+j);
if(j+1<=m&&mp[i][j+1]==0)
addedge((i-1)*m+j,(i-1)*m+j+1);
}
}
printf("%d\n",hungary(n*m));
for(int i=1;i<=n*m;i++){
if(linker[i]!=-1){
int v=linker[i];
int j=i;
if(v>j){
int temp=v;
v=j;
j=temp;
}
printf("(%d,%d)--(%d,%d)\n",(v-1)/m+1,v%m==0?m:v%m,(j-1)/m+1,j%m==0?m:j%m);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: