一开始用的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. Decorator设计模式浅谈

    装饰类跟基础组件都实现了目标接口,是为了匹配正确的类型.Java中的IO设计就是典型的Decorator设计模式. 装饰模式产生的初衷是, 对默认实现类的行为进行扩展. 由于装饰类的构造器接受的参数是 ...

  2. SQL Server 负载均衡集群方案之Moebius

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...

  3. java多线程总结四:volatile、synchronized示例

    1.synchronized保证同步 先看一个生成偶数的类 <span style="font-size:16px;">package demo.thread; /** ...

  4. 打包C#程序

    开源中国. 今天来使用VS2010对C#程序进行打包发布. 我们有一个C#程序.程序很简单,我们需要对它进行发布. Contents 步骤: 建立一个安装项目.我们得到了一个Setup1项目. 在应用 ...

  5. NSdata 与 NSString,Byte数组,UIImage 的相互转换

    1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...

  6. leetcode之Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  7. (转)Quartz.NET管理类

    最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...

  8. OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes

    1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...

  9. iPad知识点记录

    这两天玩了玩虚拟机安装Mac OS系统.iPad1的越狱以及利用iTunes将iPad2的系统升级到iOS8.1,这里将一些参考资源以及关键点记录一下. 一.虚拟机安装Mac OS 首先你的系统要能够 ...

  10. Apose 套打

    给web添加一个dll引用:Apose.Words 下载链接:http://yunpan.cn/cA7v6uceM6KVw  提取码 11df 在Global.asax里面的Application_S ...