bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093
先缩点,然后就是找最长链,DP一下即可;
注意缩点后的重边!会导致重复计算答案。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=1e5+,maxm=1e6+;
int n,m,mod,hd[maxn],ct,col[maxn],cr,tim,dfn[maxn],low[maxn],sta[maxn],top,h,t;
int f[maxn],s[maxn],siz[maxn],deg[maxn],ans,mk[maxn];
ll cnt;
bool vis[maxn];
struct N{
int fr,to,nxt;
N(int f=,int t=,int n=):fr(f),to(t),nxt(n) {}
}ed[maxm];
void add(int x,int y){ed[++ct]=N(x,y,hd[x]); hd[x]=ct;}
//void add2(int x,int y){edge[++xt]=N(y,head[x]); head[x]=xt;}
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return ret*f;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
sta[++top]=x; vis[x]=;
for(int i=hd[x],u;i;i=ed[i].nxt)
{
if(!dfn[u=ed[i].to])tarjan(u),low[x]=min(low[x],low[u]);
else if(vis[u])low[x]=min(low[x],dfn[u]);
}
if(low[x]==dfn[x])
{
cr++; int y;
while((y=sta[top])!=x)col[y]=cr,vis[y]=,siz[cr]++,top--;
col[x]=cr; vis[x]=; siz[cr]++; top--;
}
}
void topo()
{
// for(int i=1;i<=cr;i++)
// if(!deg[i])q.push(i),f[i]=siz[i],s[i]=1;
h=; t=;
for(int i=;i<=cr;i++)
if(!deg[i])sta[++t]=i,f[i]=siz[i],s[i]=;
while(h<=t)
{
// int x=q.front(); q.pop();
int x=sta[h]; h++;
for(int i=hd[x],u;i;i=ed[i].nxt)
{
deg[u=ed[i].to]--; if(!deg[u])sta[++t]=u;//
if(mk[u]==x)continue;//注意处理连通块之间的重边!
if(f[u]<f[x]+siz[u])f[u]=f[x]+siz[u],s[u]=s[x];
else if(f[u]==f[x]+siz[u])s[u]=(s[u]+s[x])%mod;
mk[u]=x;
}
}
}
int main()
{
n=rd(); m=rd(); mod=rd();
for(int i=,x,y;i<=m;i++)
{
x=rd(); y=rd();
add(x,y);
}
for(int i=;i<=n;i++)
if(!dfn[i])tarjan(i);
ct=; memset(hd,,sizeof hd);
for(int i=;i<=m;i++)
{
int u=ed[i].fr,v=ed[i].to;
if(col[u]==col[v])continue;
add(col[u],col[v]); deg[col[v]]++;
}
// for(int i=1;i<=n;i++)
// for(int j=hd[i],u;j;j=ed[j].nxt)
// {
// if(col[i]==col[u=ed[j].to])continue;
// add2(col[i],col[u]); deg[col[u]]++;
// }
topo();
for(int i=;i<=cr;i++)
{
if(f[i]>ans)ans=f[i],cnt=s[i];
else if(f[i]==ans)(cnt+=s[i])%=mod;
}
printf("%d\n%lld\n",ans,cnt);
return ;
}
bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP的更多相关文章
- BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )
WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...
- BZOJ1093: [ZJOI2007]最大半连通子图(tarjan dp)
题意 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G' ...
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
- P2272 [ZJOI2007]最大半连通子图 tarjan+DP
思路:$tarjan+DP$ 提交:1次 题解:首先对于一个强连通分量一定是一个半连通分量,并且形成的半连通分量的大小一定是它的$size$,所以我们先缩点. 这样,我们相当于要在新的$DAG$上找一 ...
- BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)
题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...
- bzoj 1093 最大半连通子图 - Tarjan - 拓扑排序 - 动态规划
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...
- BZOJ 1093 [ZJOI2007]最大半连通子图
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1986 Solved: 802[Submit][St ...
- bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2286 Solved: 897[Submit][St ...
- BZOJ 1093 最大半连通子图 题解
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2767 Solved: 1095[Submit][S ...
随机推荐
- node 第三方包学习
时间格式化 moment var moment = require('moment'); moment().format();
- 常用的SSH注解标签
常用的SSH注解标签 1.Spring的注解 关于配Bean用的 @Component @Controller @Service @Repository 作用 ...
- java编码终极探秘
首先要明白,java中string字符串都是unicode码保存的,只不过显示的时候会根据一定的规则,比如GBK或者是UTF-8去对照表中查找进行显示. 之所以会乱码就是因为使用错了编码方式. 数据是 ...
- 【译】x86程序员手册14-5.1段转换
5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...
- 如何让字体大小<12px
transform:scale( ) -webkit-transform:scale( )
- Android—修改button属性
一般安卓里的普通按钮控件灰灰的,比较单调,我们可以给按钮加上背景图片,或者自定义按钮的圆角,颜色等属性. 下面用代码举例: <Button android:id="@+id/reset ...
- Vue和JQuery相比,除了节省了开发成本,还有什么优点?
1.模块化,变量都是私有作用域,JQuery只能用全局变量.闭包,影响性能 2.组件化 3.因为1,所以方便维护 vuex 要注意刷新清空的问题 vue-router是局部刷新,window.loca ...
- Chromium CEF 2623 -- 支持 xp 的最后一个版本源码下载和编译步骤
背景 因为项目需要在客户端中内嵌浏览器,需要支持 xp 操作系统和播放视频,但 CEF 2623 以后的版本已经不支持 xp 操作系统,也不再提供 2623 版本的二进制发布包下载,只好自己手动编译. ...
- Eclipse中使用struts标签时出错
原因是Action和ActionForm对应文件中没有继承相应的类,具体来说: ActionForm的编写: 必须继承org.apache.struts.action.ActionForm Actio ...
- Problem 34
Problem 34 https://projecteuler.net/problem=34 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 1 ...