考虑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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. DNA motif 搜索算法总结

    DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  5. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. DNA解链统计物理

    来源:Kerson Huang, Lectures on Statistical Physics and Protein Folding, pp 24-25 把双链DNA解开就像拉拉链.设DNA有\( ...

  8. AC自动机+DP HDOJ 2457 DNA repair(DNA修复)

    题目链接 题意: 给n串有疾病的DNA序列,现有一串DNA序列,问最少修改几个DNA,能使新的DNA序列不含有疾病的DNA序列. 思路: 构建AC自动机,设定end结点,dp[i][j]表示长度i的前 ...

  9. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

随机推荐

  1. less 写关键帧动画

    @keyframes 关键帧动画写在less里的时候,务必要写在所有的{}之外,不能被{}包裹 甚至务必写在代码的最后 不然报错 Compilation resulted in incorrect C ...

  2. Ajax增删改查-----------增

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. file按钮美化成图片(纯HTML+CSS)

    效果图: 代码实现: html: <div> <h2>上传头像</h2> <p class="fs18">(请上传200X200px ...

  4. Linux桌面环境安装matlab并创建快捷方式

    安装matlab sudo mkdir -p /mnt/matlab sudo mount -t auto -o loop /home/chris/Downloads/2016b_linux/R201 ...

  5. rabbitmq3.7.5 centos7 集群部署笔记

    1. 准备3台 centos服务器  192.168.233.128    192.168.233.130    192.168.233.131 防火墙放开 集群端口, 这里一并把所有rabbitmq ...

  6. Log4Net帮助类

    工具类 using System; using System.Diagnostics; using log4net; namespace Trumgu_BI_PF.Util { public clas ...

  7. bzoj2969矩形粉刷

    题解: 和前面那个序列的几乎一样 容斥之后变成求不覆盖的 然后再像差分的矩形那样 由于是随便取的所以这里不用处理前缀和直接求也可以 代码: #include <bits/stdc++.h> ...

  8. [转]sqlplus /nolog 出错解决 SP2-0667: Message file sp1<lang>.msb not found SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory

    http://techxploration.blogspot.com/2012/01/resolving-sp2-0750-you-may-need-to-set.html Resolving SP2 ...

  9. SQL Server数据库存储过程中拼接字符串注意的问题

    在SQL Server数据库中书写复杂的存储过程时,一般的做法是拼接字符串,最后使用EXEC sp_executesql '拼接的字符串' 查询出结果. 先看一段代码: -- ============ ...

  10. net core体系-web应用程序-1VS2017构建一个简单的web

    使用vs2017,添加一个新项目-asp.net core web应用程序. 结构如图, wwwroot放了网站的静态资源如css.js.image文件: appsetting.json是应用程序的配 ...