UVALive 5903 Piece it together
一开始用的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的更多相关文章
- UVALive 5903 Piece it together(二分图匹配)
给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB <==碎片 W 题目一看,感觉是精确覆盖(最近 ...
- UVALive 5903 Piece it together 二分匹配,拆点 难度:1
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive 6124 Hexagon Perplexagon 题解
http://vjudge.net/problem/viewProblem.action?id=37480 East Central Regional Contest Problem C: Hexag ...
- UVALive 7261 Xiongnu's Land (扫描线)
Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...
- UVALive 6884 GREAT + SWERC = PORTO dfs模拟
题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
随机推荐
- transition和animation动画简介
本文介绍CSS动画的两大组成部分:transition和animation.我不打算给出每一条属性的详尽介绍,那样可以写一本书.这篇文章只是一个简介,帮助初学者了解全貌,同时又是一个快速指南,当你想不 ...
- 关于Extjs使用window.opener报错
项目中使用window.opener 刷新父窗口表格,父窗口表格IE8报错, window.opener.Ext.getCmp('SalesCompanyGridPanel').getStore(). ...
- 解决Redis Cluster模式下的排序问题
通常的redis排序我们可以这么做: 比如按商品价格排序:sort goods_id_set by p_*_price 这样在非集群模式下是没问题的,但如果在集群模式下,就会报错: 说是在集群模式下不 ...
- iBeacon 开发笔记
iBeacon开发笔记 2015.10.19 airlocate ========= airlocate显示如何使用这个监控范围clbeaconregions. 代码还提供了一个例子,你如何能校准和配 ...
- Lisp与JAVA的酷毙结合——abcl
最近看了一本叫做<黑客与画家>的书,其中对于Lisp语言大加褒奖.自己试着用了一下,虽然确实有反人类之嫌,但是确实是一门不错的语言,New Architect杂志上有一篇介绍ITA软件公司 ...
- 01_Java解析XML
[打印list.Map集合的工具方法] /** * 打印List集合对应的元素 */ public void printList(List<Object> list){ for(Objec ...
- HTML5入门篇
---- HTML5简介 HTML5 是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML 标准版本,现在仍处于发展阶段,但大部分浏览器已经支持某些 HTML5 ...
- PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码
项目环境是php5.3.28 项目用的ThinkPHP3.2.3 已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...
- C# 解析带前缀的Xml节点内容
一般的xml文件相信大家都会解析了,但是遇到有命名空间的带前缀的xml,对于新手可能会有点问题.我这里在论坛解答的时候就遇到过一题,见怎么获取XML节点里面的内容,在线求教.这里给大家演示一下. 他的 ...
- .NET平台开源项目速览-最快的对象映射组件Tiny Mapper之项目实践
心情小札:近期换了工作,苦逼于22:00后下班,房间一篇狼藉~ 小翠鄙视到:"你就适合生活在垃圾堆中!!!" 晚上浏览博客园 看到一篇非常实用的博客:.NET平台开源项目速览(14 ...