考虑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. Android 网络请求框架

    1.okHttp 特点 简单.灵活.无连接.无状态 优势: 谷歌官方API在6.0之后在Android SDK中移除了HttpClient,然后他火了起来, 他支持SPDY(谷歌开发的基于TCP应用层 ...

  2. |ERROR|ERROR: missing data for column "createtime" (seg3 slice1 192.168.66.23:40001 pid=33370)之mysql换行符或者空格引起的问题

    1.最近的kettle的数据交换配置,启动kettle引起的错误,如下所示: |ERROR|ERROR: missing data pid=) 引发这个错误,并不是这个字段引起的错误,一般是这个字段临 ...

  3. C#学习-类和结构

    类和结构体,对两者进行比较 语法上的区别在于,定义类要使用关键字class,而定义结构体则使用关键字struct; 结构体中不可对声明字段进行初始化,但类可以: 如果没有为类显式地定义构造函数,C#编 ...

  4. [转] Async/Await替代Promise的6个理由

    Node.js 7.6已经支持async/await了,如果你还没有试过,这篇博客将告诉你为什么要用它. Async/Await简介 对于从未听说过async/await的朋友,下面是简介: asyn ...

  5. 【BZOJ4764】弹飞大爷

    题解: 这个应该还是比较简单的 首先比较容易想到用lct来维护 我们可以建立一个特殊点 然后我们要处理环 其实只要判断它和不和这个特殊点联通就行了 那么当它不是环了我们怎么还原呢 只要对每个在根节点记 ...

  6. day4.字符串练习题

    有变量 name = “alex leNb”,完成如下操作 1. 移除name变量对应的值两边的空格,并输出处理结果 print(name.strip()) 2. 移除name变量左边的’al’并输出 ...

  7. jquery.zclip.js粘贴功能

    jquery的粘贴插件: 如下是代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...

  8. P2661 信息传递 二分图的最小环

    题目描述 有 nn 个同学(编号为 11 到 nn )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 ii 的同学的信息传递对象是编号为 T_iTi​ 的同学. 游戏开 ...

  9. 爬虫2 urllib3用法

    import urllib3 import json # 实例化一个连接池 # http = urllib3.PoolManager() # res = http.request('get','htt ...

  10. Idea中快捷键与小技巧的总结-->持续更新

    1.Scala类或单例对象中快速声明实例对象: eg. new SparkContext(conf).var 系统会自动提示,可以自动补全,如图: 2.ctrl+i与ctrl+o的区别: ctrl + ...