[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了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...
随机推荐
- js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join
js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join
- CountDownLatch & CyclicBarrier源代码实现解析
CountDownLatch CountDownLatch同意一条或者多条线程等待直至其他线程完毕以系列的操作的辅助同步器. 用一个指定的count值对CountDownLatch进行初始化. awa ...
- [React Native] Use the SafeAreaView Component in React Native for iPhone X Compatibility
In this lesson, you will learn how to use the SafeAreaView component to avoid the sensor cluster (th ...
- XMPP使用简单介绍--登录
在现阶段的通信服务中,各种标准都有.因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...
- thinkphp5项目--企业单车网站(六)
thinkphp5项目--企业单车网站(六) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...
- FTP配置说明
1.下载rpm包,如vsftpd-3.0.2-21.el7.x86_64.rpm.可在系统盘里面找到或者下载 2.参考如下步骤.或者见链接http://blog.csdn.net/uq_jin/art ...
- Linux桌面词典 星际译王(StarDict)
星际译王(StarDict)是利用GTK(GIMP TOOLKIT)开发的国际化的.跨平台的自由的桌面字典软件.它并不包含字典档,使用者须自行下载配合使用.它可以运行于多种不同的平台,如Linux, ...
- cors跨域的前端实现---根据资料整合的
1.服务端 搁response中增加Access-Control-Allow-Origin:‘*’ eg: context.Response.AddHeader("Access-Contr ...
- 洛谷 P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银)
P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银) 题目描述 Farmer John owns NN cows with spots and NN cows wi ...
- RvmTranslator6.3 is released
RvmTranslator6.3 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...