[Tarjan缩点 拓扑序DP] SRM 499 1000pts
2016-07-16 18:02
204 查看
题目大意:
题解:
题解:
<span style="font-family:Microsoft YaHei;font-size:18px;color:#000066;">#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<bitset> #define cl(x) memset(x,0,sizeof(x)) using namespace std; typedef long long ll; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } inline int read(char *s){ int len=0; char c=nc(); for (;!(c>='A' && c<='D');c=nc()); for (;c>='A' && c<='D';s[++len]=c,c=nc()); s[++len]=0; return len-1; } const int L=35; const int N=6005; long double fac[L]; inline void Pre(){ fac[0]=1; for (int i=1;i<L;i++) fac[i]=fac[i-1]*i; } struct abcd{ int a,b,c,d; abcd(int a=0,int b=0,int c=0,int d=0):a(a),b(b),c(c),d(d) { } }S ; int n,m; int icnt,back[L][L][L][L]; ll val ; struct Stack{ int s ,p; void clear(){ p=0; cl(s); } void push(int x){ s[++p]=x; } void pop(){ s[p--]=0; } int top(){ return s[p]; } bool empty(){ return p==0; } }Sta; struct edge{ int u,v; int next; }G[N<<10]; int head ,inum; bitset<N> jud ; inline void add(int u,int v,int p) { G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p; } int cnt; ll tot ; int clk,pre ,low ,scc ; #define V G[p].v inline void Tarjan(int u) { low[u]=pre[u]=++clk; Sta.push(u); for (int p=head[u];p;p=G[p].next) if (!pre[V]) Tarjan(V),low[u]=min(low[u],low[V]); else if (!scc[V]) low[u]=min(low[u],pre[V]); if (low[u]==pre[u]) { ++cnt; while (!Sta.empty()) { int x=Sta.top(); Sta.pop(); scc[x]=cnt; tot[cnt]+=val[x]; if (x==u) break; } } } int pnt,lst ; inline void dfs(int u){ pre[u]=1; for (int p=head[u];p;p=G[p].next) if (!pre[V]) dfs(V); lst[++pnt]=u; } ll f ; inline void Solve() { memset(pre,0,sizeof(pre)); for (int i=1;i<=cnt;i++) if (!pre[i]) dfs(i); reverse(lst+1,lst+pnt+1); for (int i=1;i<=cnt;i++) f[i]=tot[i]; for (int i=1;i<=pnt;i++) { int u=lst[i]; for (int p=head[u];p;p=G[p].next) f[V]=max(f[V],f[u]+tot[V]); } ll ans=0; for (int i=1;i<=cnt;i++) ans=max(ans,f[i]); printf("%lld\n",ans); } int main() { char s[L],t[L]; int len; int a,b,c,d; int a2,b2,c2,d2,a1,b1,c1,d1; freopen("return.in","r",stdin); freopen("return.out","w",stdout); Pre(); read(n); read(m); for (a=0;a<=n;a++) for (b=0;a+b<=n;b++) for (c=0;a+b+c<=n;c++) { d=n-a-b-c; S[++icnt]=abcd(a,b,c,d); val[icnt]=(ll)(fac /fac[a]/fac[b]/fac[c]/fac[d]+0.5); back[a][b][c][d]=icnt; } for (int i=1;i<=m;i++) { len=read(s); read(t); a=b=c=d=a1=b1=c1=d1=0; for (int j=1;j<=len;j++) { a-=(s[j]=='A'),b-=(s[j]=='B'),c-=(s[j]=='C'),d-=(s[j]=='D'); a1+=(s[j]=='A'),b1+=(s[j]=='B'),c1+=(s[j]=='C'),d1+=(s[j]=='D'); } for (int j=1;j<=len;j++) a+=(t[j]=='A'),b+=(t[j]=='B'),c+=(t[j]=='C'),d+=(t[j]=='D'); for (int j=1;j<=icnt;j++) if (S[j].a>=a1 && S[j].b>=b1 && S[j].c>=c1 && S[j].d>=d1) { a2=S[j].a+a; b2=S[j].b+b; c2=S[j].c+c; d2=S[j].d+d; if (j!=back[a2][b2][c2][d2] && jud[j][back[a2][b2][c2][d2]]!=1) add(j,back[a2][b2][c2][d2],++inum),jud[j][back[a2][b2][c2][d2]]=1; // add(j,back[a2][b2][c2][d2],++inum); } } for (int i=1;i<=icnt;i++) if (!pre[i]) Tarjan(i); int tem=inum; for (int i=1;i<=cnt;i++) head[i]=0; inum=0; for (int p=1;p<=tem;p++) if (scc[G[p].u]!=scc[G[p].v]) add(scc[G[p].u],scc[G[p].v],++inum); Solve(); return 0; }</span>
相关文章推荐
- hdu 5163(前缀和+分类讨论)
- 为什么要重写hashcode() 方法
- eth0,eth1,eth2,lo是什么
- 十六进制转换成十进制 二进制
- 【spring 3】AOP:静态代理
- 221. Maximal Square
- 【spring 3】AOP:静态代理
- webpack快速入门和实战
- kruskal算法求最小生成树(jungle roads的kruskal解法)
- 晶/陶振不起振及使用注意事项
- 慕课网实战—《用组件方式开发 Web App全站 》笔记七-饼图和环图组件开发
- 以后的博客可能会偏向游戏开发方向
- selenium2使用记录
- telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试
- 使用贝赛尔曲线实现仿360拖动安仔清理动画
- overwrite在hive内部表及外部表特性
- POJ 1200 Crazy Search
- pager 命令
- LTS版本的解析
- [LeetCode] 13. Roman to Integer