UVALive 5903 Piece it together

时间:2022-09-02 00:28:15

一开始用的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 二分匹配&comma;拆点 难度&colon;1

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

  3. UVALive - 3942 Remember the Word&lbrack;Trie DP&rsqb;

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

  4. 【暑假】&lbrack;实用数据结构&rsqb;UVAlive 3942 Remember the Word

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

  5. UVALive - 4108 SKYLINE&lbrack;线段树&rsqb;

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

  6. UVALive - 3942 Remember the Word&lbrack;树状数组&rsqb;

    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&&num;39&semi;s Land &lpar;扫描线&rpar;

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

  9. UVALive 6884 GREAT &plus; SWERC &equals; PORTO dfs模拟

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

随机推荐

  1. mysql的sql&lowbar;mode合理设置

    mysql的sql_mode合理设置 sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入.在生产环境必须将这个值设置为严格模式,所以 ...

  2. NumPy 学习(1): ndarrays

    Numpy 是Numerical Python的简写,用来进行高性能的科学计算以及数据分析的基础包.它是一些高级工具(pandas)的基础.它主要提供以下几个功能: (1). ndarray:计算快, ...

  3. 每天一个linux命令(40):watch命令

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  4. 答CsdnBlogger问-关于定时和后台服务问题

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 前段时间写了不少博客,在问答页面也陆续回答几十个问题,之后Csdn乙同学找到我,说要推荐我参加问答类 ...

  5. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2&period;搭建环境-2&period;1创建虚拟机

    2.1.创建虚拟机 2.1.1. 创建虚拟机节点1 2.1.2.  创建虚拟机节点2 操作如节点1. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境所有链 ...

  6. &lbrack;工具&rsqb; slf4j-api、slf4j-log4j12以及log4j之间的关系

    几乎在每个jar包里都可以看到log4j的身影,在多个子工程构成项目中,slf4j相关的冲突时不时就跳出来让你不爽,那么slf4j-api.slf4j-log4j12还有log4j是什么关系?     ...

  7. javascript类型转换、运算符、语句

    1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...

  8. 69个Spring面试题

    Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...

  9. SpringBoot配置devtools实现热部署

    spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. devtool ...

  10. HQL包含中文查询失败

    jdbc:mysql://172.16.9.6:3306/school?useUnicode=true&characterEncoding=UTF-8配置文件中的url加上编码,一般mysql ...