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/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...
随机推荐
- 7-19 求链式线性表的倒数第K项(20 分)(单链表定义与尾插法)
给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字. 输入格式: 输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理). 输出格式 ...
- node.js 写流 createWriteStream----由浅入深
createWriteStream 写流 基于fs模块 let fs=require("fs"); createWriteStream创建一个写流 我们先创建一个2.txt要写入的 ...
- litepal创建数据库表失败
今天学习郭神的litepal框架遇到了一个坑,就是程序正常跑了,但是数据库和表完全没创建!!!!!!! 先核对了litepal.xml文件,确认配置正确,assets文件夹放的也正确,最后发现竟然是因 ...
- C#中Stack<T>类的使用及部分成员函数的源代码分析
Stack<T>类 Stack<T> 作为数组来实现. Stack<T> 的容量是 Stack<T> 能够包括的元素数. 当向 Stack<T&g ...
- kvc VS kvo
Kvo是Cocoa的一个重要机制,它主要是用于对一个属性的新旧值的监控. 例如说依据A(数 据类)的某个属性值变化,B(view类)中的某个属性做出对应变化.对于MVC,kvo应用的地方很广泛. 使用 ...
- WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化
WPF中的常用布局 一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...
- ZOJ3662:Math Magic(全然背包)
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...
- QQ空间说说 视频播放
http://182.254.8.83/vwecam.gtimg.com/1006_d81d60f3c83844a5ad6a184149d4ccbb.f0.mp4?sha=78A27CF4908AB5 ...
- shell 爬虫 从日志分析到数据采集与分析
[root@VM_61_158_centos ~]# curl http://ip.chinaz.com/220.112.233.179 |grep -e Whwtdhalf.*span.*span. ...
- luence全文检索(数据库检索)
注解:从数据库中查询所有数据然后放入luence中,然后在luence来检索 package com.zhu.demo; import java.io.IOException; import java ...