bzoj1093 最大半连通子图 强连通分量&记忆化搜索
来源:互联网 发布:thinkphp socket编程 编辑:程序博客网 时间:2024/05/16 08:48
由于要使点最多,所以同一个强连通分量中的点要么都选,要么都不选。然后tarjan缩点,新点的权值为该强连通分量中点的个数。然后在新的DAG上求一条最长的链并统计最长链的个数即可。记忆化搜索解决(当然也可以bfs顺推然而我懒-_-。
下附AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define N 100005#define M 1000005using namespace std; int n,m,mod,tot,cnt,fst[N],pnt[M],nxt[M];int dfsclk,tp,stk[N],pos[N],low[N],scc[N],sum[N],f[N],g[N];bool bo[N];struct edg{ int x,y; }a[M];bool cmp(edg aa,edg bb){ return aa.x<bb.x || (aa.x==bb.x && aa.y<bb.y);}int read(){ int x=0; char ch=getchar(); while (ch<'0' || ch>'9') ch=getchar(); while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x;}void add(int aa,int bb){ pnt[++tot]=bb; nxt[tot]=fst[aa]; fst[aa]=tot;}void dfs(int x){ pos[x]=low[x]=++dfsclk; int p; stk[++tp]=x; for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; if (!pos[y]){ dfs(y); low[x]=min(low[x],low[y]); } else if (!scc[y]) low[x]=min(low[x],pos[y]); } if (low[x]==pos[x]){ cnt++; scc[x]=cnt; for (sum[cnt]=1; stk[tp]!=x; tp--){ scc[stk[tp]]=cnt; sum[cnt]++; } tp--; }}void dp(int x){ if (f[x]) return; int p; for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; dp(y); if (f[y]>f[x]){ f[x]=f[y]; g[x]=g[y]; } else if (f[y]==f[x]) g[x]=(g[x]+g[y])%mod; } if (!f[x]) g[x]=1; f[x]+=sum[x];}int main(){ n=read(); m=read(); mod=read(); int i,p; for (i=1; i<=m; i++){ int x=read(),y=read(); add(x,y); } for (i=1; i<=n; i++) if (!pos[i]) dfs(i); m=0; for (i=1; i<=n; i++) for (p=fst[i]; p; p=nxt[p]){ int j=pnt[p]; if (scc[i]!=scc[j]){ m++; a[m].x=scc[i]; a[m].y=scc[j]; } } sort(a+1,a+m+1,cmp); tot=0; memset(fst,0,sizeof(fst)); for (i=1; i<=m; i++) if (i==1 || a[i].x!=a[i-1].x || a[i].y!=a[i-1].y){ add(a[i].x,a[i].y); bo[a[i].y]=1; } for (i=1; i<=cnt; i++) if (!bo[i]) add(0,i); dp(0); printf("%d\n%d\n",f[0],g[0]); return 0;}
by lych
2015.12.4
0 0
- bzoj1093 最大半连通子图 强连通分量&记忆化搜索
- 【BZOJ1093】【ZJOI2007】最大半连通子图 强连通分量缩点+sort去重边+拓扑排序
- [bzoj1093][ZJOI2007]最大半连通子图
- bzoj1093【ZJOI2007】最大半连通子图
- [BZOJ1093][ZJOI2007]最大半连通子图
- 【bzoj1093】 [ZJOI2007]最大半连通子图
- BZOJ1093: [ZJOI2007]最大半连通子图
- BZOJ1093: [ZJOI2007]最大半连通子图
- bzoj1093: [ZJOI2007]最大半连通子图
- bzoj1093 最大半连通子图
- BZOJ1093 [ZJOI2007]最大半连通子图
- [BZOJ1093][ZJOI2007]最大半连通子图 强联通+拓扑排序+dp 做题笔记
- 【BZOJ1093】[ZJOI2007]最大半连通子图【SCC】【DAG】【DP】
- Tarjan-bzoj1093: [ZJOI2007]最大半连通子图
- [bzoj1093][ZJOI2007]最大半连通子图 Tarjan,DP
- 【BZOJ 1093】【ZJOI 2007】【最大半连通子图】【tarjan强连通分量】【拓扑排序dp】
- 【连通分量】BZOJ 1093: [ZJOI2007]最大半连通子图
- BZOJ 1093 ZJOI 2007 最大半连通子图 强联通分量+拓扑图DP
- jdbc 初始化及插入语句
- ICMP 协议基础详解
- openjudge 摘花生
- 检测手机是否越狱
- 后台定位上传的代码实践
- bzoj1093 最大半连通子图 强连通分量&记忆化搜索
- .net开发菜鸟微信之一:菜中菜.....
- malloc与free对被操作内存的影响
- ext2/3文件系统存储,查找,新建,删除文件机制详解
- JQuery 控制 checkbox 实现全选/反选功能
- 开始了我的记录生活之旅!
- 模拟也有动脑子,类似蛇形填数
- 检测调试器和trace
- 算法竞赛 之果园里的树