Hdu 1507 Uncle Tom's Inherited Land * (Binary match)

Source: Internet
Author: User

(N x M)-K <= 50 ).

Each feasible point is connected to the feasible point above and down, and a bipartite graph is obtained, and then directly

The maximum match, but the results are problematic and the analysis is not thorough. The duplicate edge is displayed, and the subscript between adjacent blocks is analyzed.

And there is a parity relationship, so only the even or odd point can be used to create a graph.

[Csharp]
# Include "stdio. h"
# Include "string. h"
# Define N 110
Struct node
{
Int x, y;
} Aa [51];
Int hash [N] [N], a [N] [N];
Int map [N] [N], v [N], mark [N];
Int n, m, cnt;
Int dx [4] = {1,-1, 0 };
Int dy [4] = {0, 0, 1,-1 };
Void get_map ()
{
Int I, j, k, x, y;
Scanf ("% d", & k );
Memset (hash, 0, sizeof (hash ));
While (k --)
{
Scanf ("% d", & x, & y );
Hash [x] [y] = 1;
}
Cnt = 1;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= m; j ++)
If (hash [I] [j] = 0)
{
A [I] [j] = cnt;
Aa [cnt]. x = I;
Aa [cnt]. y = j;
Cnt ++;
}
}
Memset (map, 0, sizeof (map ));
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= m; j ++)
If (hash [I] [j] = 0)
{
For (k = 0; k <4; k ++)
{
X = I + dx [k];
Y = j + dy [k];
If (x> = 1 & x <= n & y> = 1 & y <= m & hash [x] [y] = 0)
{
Map [a [I] [j] [a [x] [y] = 1;
Map [a [x] [y] [a [I] [j] = 1;
}
}
}
}
}
Int dfs (int k)
{
Int I;
For (I = 1; I <cnt; I ++)
{
If (aa [I]. x + aa [I]. y) % 2 = 1)
Continue;
If (map [k] [I] &! V [I])
{
V [I] = 1;
If (mark [I] =-1 | dfs (mark [I])
{
Mark [I] = k;
Return 1;
}
}
}
Return 0;
}
Void output ()
{
Int I;
For (I = 1; I <cnt; I ++)
If (mark [I]! =-1)
Printf ("(% d, % d) -- (% d, % d) \ n", aa [I]. x, aa [I]. y, aa [mark [I]. x, aa [mark [I]. y );
}
Void solve ()
{
Int I, count;
Count = 0;
Memset (mark,-1, sizeof (mark ));
For (I = 1; I <cnt; I ++)
{
If (aa [I]. x + aa [I]. y) % 2 = 0)
Continue;
Memset (v, 0, sizeof (v ));
If (dfs (I) www.2cto.com
Count ++;
}
Printf ("% d \ n", count );
Output ();
Printf ("\ n ");
}
Int main ()
{
While (scanf ("% d", & n, & m )! =-1)
{
If (! N &&! M)
Break;
Get_map ();
Solve ();
}
Return 0;
}


Author: yyf572132811

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.