bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意
两点u,v,存在一条u到v的有向路径或者从v到u的有向路径。若G'=(V',E')满足V'?V,E'是E中所有跟V'有关的边,
则称G'是G的一个导出子图。若G'是G的导出子图,且G'半连通,则称G'为G的半连通子图。若G'是G所有半连通子图
中包含节点数最多的,则称G'是G的最大半连通子图。给定一个有向图G,请求出G的最大半连通子图拥有的节点数K
,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。
Input
第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整
数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤1
00000, M ≤1000000;对于100%的数据, X ≤10^8
Output
应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.
Sample Input
1 2
2 1
1 3
2 4
5 6
6 4
Sample Output
3
program semi(input,output);
type
etype=record
t,next:longint;
end;
var
e,c:array[..]of etype;
last,dfn,low,q,hav,belong,r,a,f,g,vis:array[..]of longint;
inq:array[..]of boolean;
n,m,p,i,j,u,v,cnt,tot,top,h,t,max,ans:longint;
procedure add(u,v:longint);
begin
inc(cnt);e[cnt].t:=v;e[cnt].next:=last[u];last[u]:=cnt;
end;
procedure tarjan(k:longint);
var
i:longint;
begin
inc(cnt);dfn[k]:=cnt;low[k]:=cnt;
inc(top);q[top]:=k;inq[k]:=true;
i:=last[k];
while i<> do
begin
if dfn[e[i].t]= then begin tarjan(e[i].t);if low[e[i].t]<low[k] then low[k]:=low[e[i].t]; end
else if inq[e[i].t] and (dfn[e[i].t]<low[k]) then low[k]:=dfn[e[i].t];
i:=e[i].next;
end;
if low[k]=dfn[k] then
begin
inc(tot);hav[tot]:=;
while q[top]<>k do begin inq[q[top]]:=false;belong[q[top]]:=tot;inc(hav[tot]);dec(top); end;
dec(top);inq[k]:=false;belong[k]:=tot;inc(hav[tot]);
end;
end;
procedure ins(u,v:longint);
begin
inc(cnt);c[cnt].t:=v;c[cnt].next:=a[u];a[u]:=cnt;inc(r[v]);
end;
begin
assign(input,'semi.in');assign(output,'semi.out');reset(input);rewrite(output);
readln(n,m,p);
cnt:=;fillchar(last,sizeof(last),);
for i:= to m do begin readln(u,v);add(u,v); end;
fillchar(dfn,sizeof(dfn),);tot:=;
for i:= to n do if dfn[i]= then begin cnt:=;top:=;tarjan(i); end;
cnt:=;fillchar(a,sizeof(a),);fillchar(r,sizeof(r),);
for i:= to n do
begin
j:=last[i];
while j<> do
begin
if belong[i]<>belong[e[j].t] then ins(belong[i],belong[e[j].t]);
j:=e[j].next;
end;
end;
h:=;t:=;
for i:= to tot do
begin
if r[i]= then begin inc(t);q[t]:=i; end;
f[i]:=hav[i];g[i]:=;
end;
fillchar(vis,sizeof(vis),);
while h<t do
begin
inc(h);i:=a[q[h]];
while i<> do
begin
dec(r[c[i].t]);if r[c[i].t]= then begin inc(t);q[t]:=c[i].t; end;
if vis[c[i].t]<>q[h] then
begin
if f[q[h]]+hav[c[i].t]>f[c[i].t] then begin f[c[i].t]:=f[q[h]]+hav[c[i].t];g[c[i].t]:=g[q[h]]; end
else if f[q[h]]+hav[c[i].t]=f[c[i].t] then g[c[i].t]:=(g[c[i].t]+g[q[h]]) mod p;
vis[c[i].t]:=q[h];
end;
i:=c[i].next;
end;
end;
max:=;
for i:= to tot do if f[i]>max then begin max:=f[i];ans:=g[i]; end else if f[i]=max then ans:=(ans+g[i]) mod p;
writeln(max);writeln(ans);
close(input);close(output);
end.
bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)的更多相关文章
- 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp
题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...
- BZOJ1093: [ZJOI2007]最大半连通子图(tarjan dp)
题意 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G' ...
- bzoj 1093 最大半连通子图 - Tarjan - 拓扑排序 - 动态规划
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...
- BZOJ1093 ZJOI2007最大半连通子图(缩点+dp)
发现所谓半连通子图就是缩点后的一条链之后就是个模板题了.注意缩点后的重边.写了1h+真是没什么救了. #include<iostream> #include<cstdio> # ...
- BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )
WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
- 【tarjan 拓扑排序 dp】bzoj1093: [ZJOI2007]最大半连通子图
思维难度不大,关键考代码实现能力.一些细节还是很妙的. Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于 ...
- [luogu2272 ZJOI2007] 最大半连通子图 (tarjan缩点 拓扑排序 dp)
传送门 题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向 ...
- BZOJ1093 [ZJOI2007]最大半连通子图 【tarjan缩点 + DAG最长路计数】
题目 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G ...
随机推荐
- BZOJ1010_玩具装箱toy_KEY
题目传送门 这道题可以很快想到暴力DP的做法: f[i]=min(f[i],f[j]+(C[i]-C[j]+i-j--L)^); 但是数据范围有50000,这就需要用斜率优化了. 我们设S[i]=C[ ...
- CF833D Red-Black Cobweb
题面 题解 点分治大火题... 设白边数量为$a$,黑边为$b$,则$2min(a,b)\geq max(a,b)$ 即$2a\geq b\;\&\&2b\geq a$ 考虑点分治时如 ...
- CF 868 F. Yet Another Minimization Problem
F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...
- idea css文件不识别的问题的解决方案
可以看到 progressBar不识别 发现仅仅是这一个文件名不识别 所以换一个任意别的文件名即可识别
- php常用的几个预定义变量
__FILE__:返回所在路径文件名和文件名称 __DIR__:返回文件所在的完整目录 __LINE__:返回当前文件代码的行号 __CLASS__:返回当前类名 __FUNCTION__:返回当前方 ...
- Raft 一致性协议算法 《In search of an Understandable Consensus Algorithm (Extended Version)》
<In search of an Understandable Consensus Algorithm (Extended Version)> Raft是一种用于管理日志复制的一致性算 ...
- python之奇思妙想
一.概述 本篇主要介绍自己平常所遇到的各种有趣的关于python的简短例子 二.正文 chapter 1 解决思路: s='{:,.2f}'.format(100000.0) print(s) cod ...
- Python数据挖掘——数据概述
Python数据挖掘——数据概述 数据集由数据对象组成: 数据的基本统计描述 中心趋势度量 均值 中位数 众数 中列数 数据集的最大值和最小值的平均 度量数据分布 极差 最大值与最小值的差 四分位数 ...
- Qt 编程指南
Qt 编程指南 持续关注一本正在编写的Qt编程指南,期待作者早日完成创作.
- Wacom将在CES 2015上发布全新旗舰版Cintiq
Cintiq 27QHD和Cintiq 27QHD touch拥有宽大的工作表面,以及令人惊喜的屏幕笔触和颜色性能. 2015年1月6日,Wacom发布了Cintiq 27QHD和Cintiq 27Q ...