codechef: BINARY, Binary Movements
非常有毛病的一道题,我一个一个读字符死活过不去,改成整行整行读就 A 了...
做法就是...最小点覆盖...
我们发现可以把一个点向上跳看做被吃掉了,然后最顶层的点是无法向上跳所以不能被吃掉,然后被吃掉的点相连的边都会被删除...
这样转换完模型之后特判两下用二分图匹配就好了(因为这里的环最多是四元,或者说是偶数长度环...)
注意顶部的点必须要特判...因为顶部的点无法删除...
//by Judge
#include<cstdio>
#include<cstring>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
using namespace std;
const int N=103;
const int M=2e4+3;
typedef int MP[N][N];
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){ int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} inline int cread(int* s){ Rg int len=0; Rg char ch;
while((ch=getchar())!='O'&&ch!='.');
for(s[++len]=ch=='O';(ch=getchar())=='O'||ch=='.';s[++len]=ch=='O');
return s[len+1]='\0',len;
} char sr[1<<21],z[20];int C=-1,Z;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(Rg int x,Rg char chr=' '){
if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]=chr;
} int n,cnt,tim,res,pat,head[M<<2];
arr px,py,to,vis,bl,now,ok; MP id,mp;
struct Edge{ int to,nxt; }e[M];
inline void add(int u,int v){
e[++pat]=(Edge){v,head[u]},head[u]=pat;
}
bool dfs(int u){ vis[u]=tim;
go(u) if(vis[v]^tim){ vis[v]=tim;
if(!to[v]||dfs(to[v])) return to[v]=u,1;
} return 0;
}
int main(){
fp(i,1,read()){ n=read(),cnt=pat=res=0;
fp(i,1,n){
cread(mp[i]);
fp(j,1,n) if(mp[i][j])
id[i][j]=++cnt,
px[cnt]=i,py[cnt]=j,
bl[cnt]=(i&1);
}
fp(i,1,n+1) mp[n+1][i]=0;
memset(ok,0,(cnt+2)<<2);
memset(to,0,(cnt+2)<<2);
memset(vis,0,(cnt+2)<<2);
memset(now,0,(cnt+2)<<2);
memset(head,0,(cnt+2)<<2);
fp(i,1,n) fp(j,1,n)
if(mp[i][j]&&!mp[i-1][j-1]&&!mp[i-1][j+1]){
now[id[i][j]]=1;
if(mp[i+1][j+1]) now[id[i+1][j+1]]=2;
if(mp[i+1][j-1]) now[id[i+1][j-1]]=2;
}
fp(i,1,cnt) res+=(now[i]==2);
for(Rg int i=1;i<=n;i+=2) fp(j,1,n)
if(mp[i][j]&&!now[id[i][j]]){
if(mp[i-1][j-1]&&!now[id[i-1][j-1]])
add(id[i][j],id[i-1][j-1]);
if(mp[i-1][j+1]&&!now[id[i-1][j+1]])
add(id[i][j],id[i-1][j+1]);
if(mp[i+1][j-1]&&!now[id[i+1][j-1]])
add(id[i][j],id[i+1][j-1]);
if(mp[i+1][j+1]&&!now[id[i+1][j+1]])
add(id[i][j],id[i+1][j+1]);
}
tim=0;
fp(i,1,cnt) if(!now[i])
if(++tim&&dfs(i)) ++res;
fp(i,1,cnt) if(to[i]) ok[to[i]]=1;
++tim;
fp(i,1,cnt) if(!now[i]&&bl[i]&&!ok[i]) dfs(i);
fp(i,1,cnt) if(!now[i]&&bl[i]&&vis[i]^tim) now[i]=2;
fp(i,1,cnt) if(!now[i]&&!bl[i]&&vis[i]==tim) now[i]=2;
print(res,'\n');
fd(i,cnt,1) if(now[i]&2){
Rg int x=px[i],y=py[i]; print(x),print(y);
sr[++C]=mp[x-1][y-1]?'L':'R',sr[++C]='\n';
}
} return Ot(),0;
}
/*
2
7
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
3
.O.
O.O
...
*/
codechef: BINARY, Binary Movements的更多相关文章
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- leetcode:Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- leetcode:Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1即反转二叉树,代码如下: /** * Defin ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal
题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...
- [LeetCode]题解(python):099-Recover Binary Search Tree
题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...
随机推荐
- restful(3):认证、权限、频率 & 解析器、路由控制、分页、渲染器、版本
models.py中: class UserInfo(models.Model): name = models.CharField(max_length=32) psw = models.CharFi ...
- [NOIP2006] 提高组 洛谷P1065 作业调度方案
题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作,我们用记号j-k表示一个 ...
- sqlserver2008 存储过程使用表参数
----首先,我们定义一个表值参数类型,其实就是一个表变量 Create type dbo.tp_Demo_MultiRowsInsert as Table ( [PName] [Nvar ...
- Spring Boot实现跨域(转)
一.方法: 服务端设置Respone Header头中Access-Control-Allow-Origin 配合前台使用jsonp 继承WebMvcConfigurerAdapter 添加配置类 二 ...
- RelativeLayout不能居中的解决的方法
在LinearLayout中有个让元素居中的办法就是.比方在LinearLayout里有个TextView.设置TextView的gravity能够让其居中. 而在Realative里设置这个不起作用 ...
- frameset怎样实现整个页面的跳转
登录页面login.jsp,系统登录成功后展示mainLayout.jsp, 我如今用frameset框架把页面mainLayout.jsp分为三部分,head.jsp..left.jsp.right ...
- kvc kvo 总结---180313
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- lambda和委托那点事
Lambda 简介 Lambda 表达式是一种可用于创建委托或表达式目录树类型的一种匿名函数(匿名方法+Lambda).通过使用 lambda 表达式,可以写入可作为参数传递或作为函数 调用值返回的本 ...
- 使用 Code Map 理解复杂代码1 ——Visual Studio2012
第一次知道code map是在Visual Studio Ultimate 2012自带的解说上面,当时认为十分好奇,所以查了查.结果一查就是好几天.原来Visual Studio Ultimate ...
- java后台修改ZK页面的title
Clients.evalJavaScript("document.title='"+basicDBObject.getString("systemName")+ ...