[NOI.AC#35]string 缩点+拓扑排序
因为有交换相邻字母,因此给你字符串就相当于给你了这个字符串的所有排列
把等价的串映射到整数范围,再根据 \(m\) 种魔法连边,缩点后在 DAG 上DP即可
无耻地用了int128
#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef __int128 ll;
typedef unsigned int uint;
typedef unsigned long long ull;
template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
const int N=52,MN=N*N*N;
int n,m,c1[N<<1][4],c2[N<<1][4],num[N][N][N],T;
ll C[N][N],cnt[MN],val[MN],f[MN],ans;
char s1[N],s2[N];
int head[MN],fr[MN*N],to[MN*N],nxt[MN*N],tot;
inline void add(int x,int y){if(x==y)return;fr[++tot]=x,to[tot]=y,nxt[tot]=head[x];head[x]=tot;}
int s[MN],top,dfn[MN],dfs_clock,bel[MN],scc;
bool ins[MN];
int dfs(int x){
int low=dfn[x]=++dfs_clock;
s[++top]=x,ins[x]=1;
for(int i=head[x];i;i=nxt[i]){
const int&y=to[i];
if(!dfn[y])smin(low,dfs(y));
else if(ins[y])smin(low,dfn[y]);
}
if(low==dfn[x]){
++scc;
do bel[s[top]]=scc,val[scc]+=cnt[s[top]],ins[s[top]]=0;while(s[top--]!=x);
}
return low;
}
int ind[MN],q[MN];
void toposort(){
int l=1,r=0,x;
REP(i,1,scc)if(!ind[i])q[++r]=i;
while(l<=r){
x=q[l++];f[x]+=val[x];smax(ans,f[x]);
for(int i=head[x];i;i=nxt[i]){
smax(f[to[i]],f[x]);
if(!--ind[to[i]])q[++r]=to[i];
}
}
}
template<typename Tp>inline void print(Tp x){
static char s[50];
int top=0;
for(;x;x/=10)s[++top]=x%10^'0';
while(top)putchar(s[top--]);
}
int main(){
scanf("%d%d",&n,&m);
REP(i,1,m){
scanf("%s%s",s1+1,s2+1);int len=strlen(s1+1);
REP(j,1,len)++c1[i][s1[j]-'A'],++c2[i][s2[j]-'A'];
}
C[0][0]=1;
REP(i,1,n){
C[i][0]=1;
REP(j,1,i)C[i][j]=C[i-1][j]+C[i-1][j-1];
}
REP(i,0,n)REP(j,0,n-i)REP(k,0,n-i-j){
num[i][j][k]=++T;
cnt[T]=C[n][i]*C[n-i][j]*C[n-i-j][k];
}
REP(a,0,n)REP(b,0,n-a)REP(c,0,n-a-b){
int d=n-a-b-c;
REP(i,1,m)if(a>=c1[i][0]&&b>=c1[i][1]&&c>=c1[i][2]&&d>=c1[i][3]){
int e,f,g,h;
e=a-c1[i][0]+c2[i][0];
f=b-c1[i][1]+c2[i][1];
g=c-c1[i][2]+c2[i][2];
add(num[a][b][c],num[e][f][g]);
}
}
REP(i,1,T)if(!dfn[i])dfs(i);
int all=tot;tot=0;memset(head,0,sizeof head);
REP(i,1,all){
int&bx=bel[fr[i]],&by=bel[to[i]];
if(bx!=by)add(bx,by),++ind[by];
}
toposort();print(ans);
return 0;
}
[NOI.AC#35]string 缩点+拓扑排序的更多相关文章
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
- [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序
---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...
- poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- hdu 1811(缩点+拓扑排序+并查集)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
- poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...
- 2018.11.06 bzoj1093: [ZJOI2007]最大半连通子图(缩点+拓扑排序)
传送门 先将原图缩点,缩掉之后的点权就是连通块大小. 然后用拓扑排序统计最长链数就行了. 自己yyyyyy了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...
随机推荐
- SPOJ 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- Looger级别
Logger级别 日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容 ...
- 把握linux内核设计思想(三):下半部机制之软中断
[版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途] 中断处理程序以异步方式执行,其会打断其它重要代码,其执行时该中 ...
- Tensorflow MNIST 数据集測试代码入门
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...
- 从Linux系统内存逐步认识Android应用内存
总述 Android应用程序被限制了内存使用上限,一般为16M或24M(具体看系统设置),当应用的使用内存超过这个上限时,就会被系统认为内存泄漏,被kill掉.所以在Android开发时,管理好内存的 ...
- Spring Security Architecture--官方
原文地址:https://spring.io/guides/topicals/spring-security-architecture/ Table of contents Authenticatio ...
- OPENCV(3) —— 对XML和YAML文件实现I/O 操作
XML\YAML文件在OpenCV中的数据结构为FileStorage string filename = "I.xml"; FileStorage fs(filename, Fi ...
- jquery常规选择器再学习_1123
jquery选择器基本模拟css语法来获取元素: 1 常规选择器 id 常见的元素标签 class 2 进阶选择器 组合选择器 常规选择器多个组合在一起 通配符选择器 * ,通常用于局部环境下 后代选 ...
- SGU 323 Aviamachinations
Aviamachinations Time Limit: 4500ms Memory Limit: 65536KB This problem will be judged on SGU. Origin ...
- 手动脱FSG壳实战
作者:Fly2015 对于FSG壳.之前没有接触过是第一次接触.这次拿来脱壳的程序仍然是吾爱破解论坛破解培训的作业3的程序.对于这个壳折腾了一会儿,后来还是被搞定了. 1.查壳 首先对该程序(吾爱破解 ...