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 ...
随机推荐
- 【sqli-labs】 less59 GET -Challenge -Double Query -5 queries allowed -Variation2 (GET型 挑战 双查询 只允许5次查询 变化2)
整型的注入 http://192.168.136.128/sqli-labs-master/Less-59/?id=1 or UpdateXml(1,concat(0x7e,database(),0x ...
- Vue项目在IE浏览器报错polyfill-eventsource added missing EventSource to window
已经安装了babel-polyfill,依然报错.
- CAD在图纸保存的同时,也把基本信息保存了(网页版)
主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgToURLEx 保存DWG文件到服务器上的扩展函数.详细说明如下: 参数 说明 pszServerUrl 服务器网 ...
- ie 浏览器下ajax请求来自缓存的解决方法
如上图所示,在ie浏览器下发出的请求,如何缓存中已经出现过这条请求记录,则不会请求服务端数据,解决方法是在请求后增加一个随机数,使每次请求都不同*可以添加当前时间戳 url+'?t='+Date.no ...
- Asp.NET误人子弟教程:在MVC里面结合JQ实现AJAX
public class Person { public string Name { get; set; } public string City { get; set; } public strin ...
- Python基础-判断闰年
输入一个年份,判断该年份是否是闰年并输出结果.求它是否是闰年?要求:能被4整除不能被100整除或者能被400整除. y=input('请输入年份:') if(y%4==0 and y%100 != 0 ...
- sdp概览
sdp会话描述符有多行用如下格式组成的文本: <type>=<value>等号旁边不允许留白. sdp会话描述符有一个session-level的段,后面会接零个或者多个med ...
- 【模板】最小生成树Kruskal
洛谷3366 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; ,ans=; struct e ...
- 彻底搞定Android开发中软键盘的常见问题
软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog. InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参 ...
- win7安装gmpy2
1.下载地址:https://pypi.python.org/pypi/gmpy2 2.安装python和pip python 安装 下载: https://www.python.org/getit/ ...