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 ...
随机推荐
- Python(2.7.6) 迭代器
除了对列表.集合和字典等进行迭代,还能对其他对象进行迭代:实现 __iter__ 方法的对象.例如, 文件对象就是可迭代的: >>> dir(file) ['__class__', ...
- hadoop的mapreduce过程
http://www.cnblogs.com/sharpxiajun/p/3151395.html 下面我从逻辑实体的角度讲解mapreduce运行机制,这些按照时间顺序包括:输入分片(input s ...
- asp快速开发方法之分页函数
log_Content "自己常用的ASP分页代码,将以下代码放入你的函数文件内,在使用的文件内写上<!--#include file="调用文件.asp" /&g ...
- C# ACM poj1002
排序 public static void acm1002(string[] azx) { string[] a = new string[azx.Length]; ; i < azx.Leng ...
- 解决VS2012【加载......符号缓慢】的问题
http://blog.csdn.net/shi0090/article/details/19411777 最近在用VS2012调试时,经常出现"加载......符号缓慢的问题", ...
- Pascal Language: Recommended Materials
Recommended materials: http://www.marcocantu.com/epascal/
- 第30条:用enum代替int常量
在java1.5之前,表示枚举类型的常用模式是声明一组具名的int常量,每个类型成员一个常量: public static final int APPLE_FUJI = 0; public stati ...
- [翻译][MVC 5 + EF 6] 5:Code First数据库迁移与程序部署
原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 1.启用 ...
- linux系统制作简单流程
制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...
- Linux FTP服务安装和远程登录失败
问题:本机VPlayer安装pure-ftpd ftp服务,通过flashfxp从windows连接出现以下错误: [左] 正在连接到 vmare -> IP=192.168.174.133 ...