[BZOJ 1093 && YZOI1172] Tarjan缩点+拓扑DP 最大半连通子图

11 篇文章 0 订阅
8 篇文章 0 订阅

A了一道BZOJ上的ZJOI题。。

啧啧ZJOI。。。。可惜一定是ZJOI的签到题。。。

1172.png
给定一个有向图G,请求出G的最大半连通子图拥有的节点数K,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。


先Tarjan缩点。。得到一个DAG..

然后我们可以yy得到最大半连通子图一定是这个DAG上的最长链。。

由于是一个DAG,所以可以一边拓扑一边DP。。

由于构图出来有重边,所以用BFS。。

突然明白了topsort好像也可以借助BFS达到O(n)?..

最近真是脑洞大开23333

const   maxn=100001;
      maxm=1000001;
var du,count,stack,belong,dfn,low,headlist:array[0..maxn] of longint;
    next,t:array[0..maxm] of longint;
    scc_cnt,top,v,time,num,m,i,j,k,n,x,y,modn:longint;
    vis,ins:array[0..maxn] of boolean;
    f,g,sign,head:array[0..maxn] of longint;
    nextt,tt:array[0..maxm] of longint;
    queue:array[0..maxn] of longint;
    ans,tot,front,finish:longint;
procedure init;
begin
    readln(n,m,modn);
    for i:=1 to n do headlist[i]:=-1;
    for i:=1 to m do
    begin
        readln(x,y);
        inc(num);
        next[num]:=headlist[x];
        headlist[x]:=num;
        t[num]:=y;
    end;
end;
function min(a,b:longint):longint;inline;
begin
    if a<b then exit(A);
    exit(b);
end;
procedure tarjan(u:longint);
var i:longint;
begin
    inc(time);
    dfn[u]:=time; low[u]:=time;
    inc(top);
    stack[top]:=u;
    vis[u]:=true; ins[u]:=true;
    i:=headlist[u];
    while i<>-1 do
    begin
        if not(vis[t[i]]) then begin
                                tarjan(t[i]);
                                low[u]:=min(low[u],low[t[i]]);
                               end
                          else if ins[t[i]] then low[u]:=min(low[u],dfn[t[i]]);
        i:=next[i];
    end;
    if dfn[u]=low[u] then begin
                            inc(scc_cnt);
                            repeat
                                v:=stack[top];
                                dec(top);
                                ins[v]:=false;
                                belong[v]:=scc_cnt;
                                inc(count[scc_cnt]);
                            until u=v;
                          end;
end;
procedure got;
begin
    for i:=1 to scc_cnt do head[i]:=-1;
    for x:=1 to n do
    begin
        i:=headlist[x];
        while i<>-1 do
        begin
            if belong[x]<>belong[t[i]] then begin
                                                inc(num);
                                                inc(du[belong[t[i]]]);
                                                nextt[num]:=head[belong[x]];
                                                head[belong[x]]:=num;
                                                tt[num]:=belong[t[i]];
                                            end;
            i:=next[i];
        end;
    end;
end;
procedure topsort;
begin
    front:=0; finish:=0;
    for i:=1 to scc_cnt do
        if du[i]=0 then begin
                            inc(finish);
                            f[i]:=count[i];
                            g[i]:=1;
                            queue[finish]:=i;
                        end;
    while front<>finish do
    begin
        inc(front);
        i:=head[queue[front]];
        while i<>-1 do
        begin
            if sign[tt[i]]<>queue[front] then begin
                                                if f[tt[i]]=f[queue[front]]+count[tt[i]] then g[tt[i]]:=(g[tt[i]]+g[queue[front]]) mod modn;
                                                if f[tt[i]]<f[queue[front]]+count[tt[i]] then begin
                                                                                    f[tt[i]]:=f[queue[front]]+count[tt[i]];
                                                                                    g[tt[i]]:=g[queue[front]];
                                                                                 end;
                                                sign[tt[i]]:=queue[front];
                                             end;
            dec(du[tt[i]]);
            if du[tt[i]]=0 then begin
                                    inc(finish);
                                    queue[finish]:=tt[i];
                               end;
            i:=nextt[i];
        end;
    end;
end;
procedure print;
begin
    for i:=1 to scc_cnt do
    begin
        if ans=f[i] then tot:=(tot+g[i]) mod modn;
        if ans<f[i] then begin
                            ans:=f[i];
                            tot:=g[i];
                         end;
    end;
    writeln(ans);
    writeln(tot);
end;
procedure main;
begin
    init;
    for i:=1 to n do if not(vis[i]) then tarjan(i);
    num:=0;
    got;
    topsort;
    print;
end;
begin
    main;
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值