考虑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. IEDA序列化设置

  2. settings.py常见配置项

    settings.py常见配置项 1. 配置Django_Admin依照中文界面显示 LANGUAGE_CODE = 'zh-hans' 2. 数据库配置(默认使用sqlite3) 1 .默认使用的s ...

  3. 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. 基于容器的ETCD集群脚本

    其实是从上一篇的脚本里剥离出来的. 加深一下印象吧. docker run \ -d \ -p ${ETCD_CLI_PORT}:${ETCD_CLI_PORT} \ -p ${ETCD_CLU_PO ...

  5. (Access denied for user 'root'@'slaver1' (using password: YES))

    1.问题描述,启动azkaban的时候报如下所示的错误.之前使用azkaban是root用户,今天使用hadoop用户进行配置和使用,报这个错,说是root连接mysql拒绝了. [hadoop@sl ...

  6. java.lang.OutOfMemoryError: unable to create new native thread

    ps -o nlwp 70753 sudo -u tomcat jmap -dump:format=b,file=fundmarketmanage.hprof 78894

  7. Hyper-V 替换 vmwp

    要激活 Hyper-V 下的虚机 最简单的方法是用带证书的vmwp替换掉原来的 带证书的vmwp参见:http://bbs.pcbeta.com/viewthread-1408240-1-1.html ...

  8. [转] 2016 JavaScript 发展现状大调查

    有人认为JavaScript是最好的语言,有人认为它一团糟.可按照C++之父的话来讲: 世界上只有两种编程语言:一种是天天被人喷的,另一种是没人用的. 不论你喜欢承认与否,JavaScript已经一天 ...

  9. asp.net core 创建允许跨域请求的api, cors.

    配置应用方域名. 在webapi中引用cors包,在startup的Configure\ConfigServices中增加启动项配置,在api中增加EnableCors的Attribute属性.即可. ...

  10. 考虑实现一个不抛异常的swap

    Effective C++:参考自harttle land 类的swap实现与STL容器是一致的:提供swap成员函数, 并特化std::swap来调用那个成员函数. class Widget { p ...