一开始用的STL一直超时不能过,后来发现AC的代码基本都用的普通邻接表,然后改了一下13s,T=T,效率太低了。然后把某大神,详情戳链接http://acm.hust.edu.cn/vjudge/problem/viewSource.action?id=1199083的300+ms的代码加上自己的优化成功到了85ms,限时30s的程序还是挺有成就感的。

题目分析:

一个黑格子要和相邻的两个白格子构成一块,也就是成直角形状,因此,每个黑块必须和相邻上下白块中的一个匹配,同样左右白块必须也有一个和它匹配,将黑块拆成两个点, 这样问题就变成二分图的匹配问题了。

我的TLE的代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mp(a, b) make_pair((a), (b))
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define stop system("pause");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT;
const int maxn = 555;
char maze[maxn][maxn];
int R, C;
int flag[maxn * maxn];
int match[maxn * maxn];
VI g[maxn * maxn];
vector<pii> BLK;
bool dfs(int x)
{
for(int i = 0; i < g[x].size(); i++)
{
int v = g[x][i];
if(!flag[v])
{
flag[v] = 1;
if(match[v] == -1 || dfs(match[v]))
{
match[v] = x;
return true;
}
}
}
return false;
}
int main(void)
{
int T, t;
scanf("%d", &T);
memset(maze, -1, sizeof(maze));
for(t = 1; t <= T; t++)
{
int b = 0, w = 0;
BLK.clear();
for(int i = 0; i < maxn*maxn; i++)
g[i].clear();
scanf("%d%d", &R, &C);
while(getchar() != '\n') ;
for(int i = 1; i <= R; i++)
{
scanf("%s", maze[i] + 1);
for(int j = 1; j <= C; j++)
if(maze[i][j] == 'B')
b++, BLK.pb(mp(i, j));
else if(maze[i][j] == 'W')
w++;
}
if(b*2 != w)
puts("NO");
else
{
for(int i = 0; i < BLK.size(); i++)
{
int x = BLK[i].first;
int y = BLK[i].second;
if(maze[x-1][y] == 'W')
g[i].pb((x-1)*C+y);
if(maze[x+1][y] == 'W')
g[i].pb((x+1)*C+y);
if(maze[x][y-1] == 'W')
g[i+b].pb(x*C+y-1);
if(maze[x][y+1] == 'W')
g[i+b].pb(x*C+y+1);
}
memset(match, -1, sizeof(match));
int i;
for( i = 0; i < b; i++)
{
memset(flag, 0, sizeof(flag));
if(!dfs(i) || !dfs(i+b))
break;
}
if(i < b)
puts("NO");
else puts("YES");
}
}
return 0;
}

改后的代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mp(a, b) make_pair((a), (b))
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define stop system("pause");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VII;
typedef vector<pii, int> VIII;
typedef VI:: iterator IT;
int n,m;
char s[600][600];
int B[600][600];
int W[600][600];
int link[300000];
int vis[300000];
int b,w;
VII BLK;
struct bian
{
int v,next;
}e[1000000];
int head[300000];
int num;
int now;
void add(int u,int v)
{
e[num].v=v;
e[num].next=head[u];
head[u]=num++;
}
bool dfs(int k)
{
for(int h=head[k];h!=-1;h=e[h].next)
{
int v=e[h].v;
if(vis[v]==now)
continue;
vis[v]=now;
if(link[v]==-1||dfs(link[v]))
{
link[v]=k;
return 1;
}
}
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
BLK.clear();
num=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
memset(s,0,sizeof(s));
for(int i=0;i<n;i++)
scanf("%s",s[i]);
b=w=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(s[i][j]=='B')
B[i][j]=++b, BLK.pb(mp(i, j));
if(s[i][j]=='W')
W[i][j]=++w;
}
if(b*2!=w)
{
puts("NO");
continue;
}
else
{
for(int k = 0; k < BLK.size(); k++)
{
int i = BLK[k].first;
int j = BLK[k].second;
if(s[i][j]=='B')
{
if(s[i-1][j]=='W')
add(B[i][j],W[i-1][j]);
if(s[i+1][j]=='W')
add(B[i][j],W[i+1][j]);
if(s[i][j-1]=='W')
add(B[i][j]+b,W[i][j-1]);
if(s[i][j+1]=='W')
add(B[i][j]+b,W[i][j+1]);
}
} memset(link,-1,sizeof(link)) ;
memset(vis,0,sizeof(vis));
int i;
for(i=1;i<=b*2;i++)
{
now=i;
if(!dfs(i))
break;
}
if(i > b*2)
puts("YES");
else
puts("NO");
}
}
return 0;
}

UVALive 5903 Piece it together的更多相关文章

  1. UVALive 5903 Piece it together(二分图匹配)

    给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB  <==碎片 W 题目一看,感觉是精确覆盖(最近 ...

  2. UVALive 5903 Piece it together 二分匹配,拆点 难度:1

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  5. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  6. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  7. UVALive 6124 Hexagon Perplexagon 题解

    http://vjudge.net/problem/viewProblem.action?id=37480 East Central Regional Contest Problem C: Hexag ...

  8. UVALive 7261 Xiongnu's Land (扫描线)

    Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...

  9. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

随机推荐

  1. php图片等比例缩放

    新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称) 源代码如下: <?php$filename="q.jpg"; $per=0. ...

  2. xml的语法与创建

    xml语法很简单,但很严格,如果出现错误则不能正常解析,而HTML如果出现局部的错误,照样解析 xml第一行必须写xml头<?xml version='1.0' encoding='utf8'? ...

  3. C#学习笔记5:数据类型与进制转换

    1.C#: 8种整形类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十进制浮点类型. 2.Decimal是一种特殊的浮点类型,能够存储大数字而无表示错误. 常用于货币计算.在表示的所有十进制 ...

  4. android N刷机

    1. 打开开发者选项2. 在开发者选项页面打开 oem解锁3. 运行 adb reboot bootloader4. 运行 fastboot flashing unlock5. 解压你下载的刷机包 6 ...

  5. RDLC打印或导出Word的 分页设置 页边距和页面大小

    RDLC 导出Word的时候发现,Word的尺寸和页边距有问题,查了MSDN看到这样一段话 Page Sizing When the report is rendered, the Word page ...

  6. [.Net MVC] 过滤器以及异常处理

    项目:后台管理平台 意义:程序发布后,不应该对用户显示因程序出错和崩溃而出现的错误信息,采用统一友好的错误页面,并将错误信息记录到日志中供管理人员查看. 一.过滤器Filter Filter(筛选器) ...

  7. Java线程练习

    /*线程练习创建两个线程,与主线程交替运行 */ class Text extends Thread{    private String name;    Text(String name)     ...

  8. Update msi using vbscript

    参考: http://stackoverflow.com/questions/1609250/how-do-i-add-update-a-property-inside-an-msi-from-the ...

  9. [Guava官方文档翻译] 5. Guava的Object公共方法 (Common Object Utilities Explained)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3537367.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  10. [翻译][MVC 5 + EF 6] 7:加载相关数据

    原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 1.延迟(Lazy)加载.预先(Eage ...