BZOJ2255 : [Swerc2010]Palindromic DNA
考虑2-SAT建图,设$a[i][0..1]$表示$i$变不变,$b[i][0..1]$表示$i$是下降还是上升。
首先相邻的不能同时动,说明$a[i]$和$a[i+1]$里最多选一个。
对于$x$和$y$要相等,假设$s[x]\geq s[y]$。
$1.$若$s[x]-s[y]=3$,则视为$1$,并交换$x,y$。
$2.$若$s[x]=s[y]$,那么它们的任何行动都是相等的:
$a[x][0]\leftrightarrow a[y][0]$
$a[x][1]\leftrightarrow a[y][1]$
$b[x][0]\leftrightarrow b[y][0]$
$b[x][1]\leftrightarrow b[y][1]$
$3.$若$s[x]-s[y]=1$,那么它们有且仅能动一个,方向也是定的:
$a[x][0]\leftrightarrow a[y][1]$
$a[x][1]\leftrightarrow a[y][0]$
$a[x][1]$和$b[x][0]$最多只能选一个
$a[y][1]$和$b[y][1]$最多只能选一个
$4.$若$s[x]-s[y]=2$,那么它们都要动,而且方向相反:
$a[x][0]\rightarrow a[x][1]$
$a[y][0]\rightarrow a[y][1]$
$b[x][0]\leftrightarrow b[y][1]$
$b[x][1]\leftrightarrow b[x][0]$
求出SCC,若某个$a[i][0]$和$a[i][1]$在同一个SCC则无解,不需要考虑$b$,因为可以既不上升也不下降。
时间复杂度$O(n+m)$。
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100010,M=N*4,E=2000000;
int n,m,i,j,k,cnt,a[N][2],b[N][2],p[N],g[2][M],v[2][E],nxt[2][E],ed,q[M],t,f[M];
bool vis[M];char s[N];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline int getid(char x){
if(x=='A')return 0;
if(x=='G')return 1;
if(x=='T')return 2;
return 3;
}
inline void add(int x,int y){
v[0][++ed]=y;nxt[0][ed]=g[0][x];g[0][x]=ed;
v[1][ed]=x;nxt[1][ed]=g[1][y];g[1][y]=ed;
}
inline void add2(int x,int y){add(x,y),add(y,x);}
inline void check(int x,int y){
if(s[x]<s[y])swap(x,y);
int w=s[x]-s[y];
if(w==3)w=1,swap(x,y);
if(!w){
add2(a[x][0],a[y][0]);
add2(a[x][1],a[y][1]);
add2(b[x][0],b[y][0]);
add2(b[x][1],b[y][1]);
return;
}
if(w==1){
add2(a[x][0],a[y][1]);
add2(a[x][1],a[y][0]);
add(a[x][1],b[x][0]);
add(b[x][1],a[x][0]);
add(a[y][1],b[y][1]);
add(b[y][0],a[x][0]);
return;
}
add(a[x][0],a[x][1]);
add(a[y][0],a[y][1]);
add2(b[x][0],b[y][1]);
add2(b[x][1],b[y][0]);
}
void dfs1(int x){
vis[x]=1;
for(int i=g[0][x];i;i=nxt[0][i])if(!vis[v[0][i]])dfs1(v[0][i]);
q[++t]=x;
}
void dfs2(int x,int y){
vis[x]=0;f[x]=y;
for(int i=g[1][x];i;i=nxt[1][i])if(vis[v[1][i]])dfs2(v[1][i],y);
}
int main(){
while(~scanf("%d%d",&n,&m)){
if(!n)return 0;
scanf("%s",s);
for(i=0;i<n;i++)s[i]=getid(s[i]);
for(cnt=i=0;i<n;i++)for(j=0;j<2;j++)a[i][j]=++cnt,b[i][j]=++cnt;
for(ed=0,i=1;i<=cnt;i++)g[0][i]=g[1][i]=0;
for(i=0;i<n;i++){
if(i)add(a[i][1],a[i-1][0]);
if(i+1<n)add(a[i][1],a[i+1][0]);
}
while(m--){
read(k);
for(i=0;i<k;i++)read(p[i]);
for(i=0,j=k-1;i<j;i++,j--)check(p[i],p[j]);
}
for(t=0,i=1;i<=cnt;i++)if(!vis[i])dfs1(i);
for(i=cnt;i;i--)if(vis[q[i]])dfs2(q[i],q[i]);
for(i=0;i<n;i++)if(f[a[i][0]]==f[a[i][1]])break;
puts(i<n?"NO":"YES");
}
}
BZOJ2255 : [Swerc2010]Palindromic DNA的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- DNA motif 搜索算法总结
DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- DNA解链统计物理
来源:Kerson Huang, Lectures on Statistical Physics and Protein Folding, pp 24-25 把双链DNA解开就像拉拉链.设DNA有\( ...
- AC自动机+DP HDOJ 2457 DNA repair(DNA修复)
题目链接 题意: 给n串有疾病的DNA序列,现有一串DNA序列,问最少修改几个DNA,能使新的DNA序列不含有疾病的DNA序列. 思路: 构建AC自动机,设定end结点,dp[i][j]表示长度i的前 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
随机推荐
- IDEA复制项目操作
- pandas之whl格式安装
一.主要介绍linux下安装 1.下载安装包:https://pypi.doubanio.com/simple/pandas/ 2.安装 pip install wheel 3.更新一下pip版本( ...
- 将txt文本转换为excel格式
将txt文本转换为excel格式,中间使用的列分割为 tab 键 一.使用xlwt模块 注:Excel 2003 一个工作表行数限制65536,列数限制256 需要模块:xlwt 模块安装:xlwt ...
- Scrapy 框架 安装
Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...
- Memcached 快速入门
Memcached简介 Memcached是一个专门用来做缓存的服务器,而且缓存的数据都在内存中.Memcached就相当于一个Dictionary键值对集合,保存的是键值对,然后根据key取valu ...
- fcagte.exe应用程序错误
原文:What is Fcagte.exe and How To Fix It? Overview of Fcagte.exe What Is Fcagte.exe? Fcagte.exe is a ...
- Redis cluster集群模式的原理
redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内 ...
- Vuex详解笔记2
关于 state 每个vuex 应用只有一个 store 实例,所以使用起来不会太复杂,对于定位错误状态和操作会很方便. 简单用法:在vuex 的计算属性中返回vuex 的状态 最基本的使用方式,通过 ...
- C# 之 提高WebService性能大数据量网络传输处理
1.直接返回DataSet对象 特点:通常组件化的处理机制,不加任何修饰及处理: 优点:代码精减.易于处理,小数据量处理较快: 缺点:大数据量的传递处理慢,消耗网络资源: 建议:当应用系统在内网.专网 ...
- 配置文件——节点<machineKey>的作用,强随机生成
<machineKey>这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NET自动使用它来保护表单验证Cookie,你也可以将它用于受保护的视图状态数据.同时,这个密 ...