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 ...
随机推荐
- VOC数据集生成代码使用说明
#split.py 文件 输入格式为images ,和标签txt文件,txt中的数据为坐标值共8个. import os import numpy as np import math import c ...
- ubuntu安装界面 会出现不完整情况
解决方法: alt+鼠标左键或者win+鼠标左键拖动
- python---顺序查找,二分查找
比较熟悉了. 但要注意细节, 二分查找时,普通方法mid处理,递归时,mid处理. # coding = utf-8 def sequential_search(a_list, item): pos ...
- bzoj2961 共点圆 bzoj 4140
题解: 比较水的一道题 首先我们化简一下式子发现是维护xxo+yyo的最值 显然是用凸包来做 我们可以直接用支持插入删除的凸包 也是nlogn的 因为没有强制在线,我们也可以cdq,考虑前面一半对答案 ...
- Busybox制作ARM(iTOP4412) 根文件系统
本记录来源与自身操作过程. 1.制作环境 PC环境:外部Winows8.1 内部则为vmware11+ubuntu12.04 嵌入式设备:iTOP4412 交叉工具:arm-none-linux-gn ...
- [转]PO BO VO DTO POJO DAO概念及其作用(附转换图)
来源:http://www.blogjava.net/vip01/archive/2013/05/25/92430.html J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候 ...
- C# 之 索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- 【AtCoder】AGC016
A - Shrinking 用每个字母模拟一下就行 #include <bits/stdc++.h> #define fi first #define se second #define ...
- Zk 集群概念
https://blog.csdn.net/gs80140/article/details/51496925
- xml方式将dataset导出excel
using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...