首先比较明显的是如果存在一个半连通子图,我们将其中的环缩成点,那么该图仍为半连通子图,这样我们就可以先将整张图缩点,重新构图,新图为拓扑图,记录每个新的点表示的强连通分量中点的个数num[i],那么我们就可以DP了,新图中的每一条链都为原图的半连通子图,这样我们找到新图中的最长链就行了,找入度为0的点dfs做树上DP,这样我们可以知道每个点的len[i]代表从这个点开始的最长链的长度,len[i]=max(len[son of i])+num[i],然后我们求出来了第一问,对于第二问,我们需要找len[i]=ans1的点做dfs,然后设ans[i]为以i为根的子树的方案数,那么ans[i]+=ans[son of i] (len[i]=len[son of i]+num[i]),因为我们需要找最长链上的点来更新答案,这样最后再累计答案就好了。

  反思:开始题中说没有重边,但是没有考虑到重构图之后的图是可能有重边的,这样第二问的答案就可能会被重复累加,所以我们DP的时候可以维护一个栈,和每个栈中元素的父亲,这样对于一个点枚举子节点,如果子节点没有在栈中出现过,那么就累加答案,该子节点进栈,dfs最后的时候再弹出所有栈中x的子节点。

 

/**************************************************************
    Problem: 1093
    User: BLADEVIL
    Language: C++
    Result: Accepted
    Time:1992 ms
    Memory:45028 kb
****************************************************************/
 
//By BLADEVIL
#include <cstdio>
#include <algorithm>
#define maxn 200020
#define maxm 4000040
 
using namespace std;
 
int n,m,d39,l,tot,time,size;
int last[maxn],other[maxm],pre[maxm],stack[maxn],low[maxn],dfn[maxn],flag[maxn],col[maxn],num[maxn],in[maxn];
int len[maxn],ans[maxn],father[maxn];
int p1,p2;
 
void connect(int x,int y){
    pre[++l]=last[x];
    last[x]=l;
    other[l]=y;
    //if (x>n||y>n) printf("%d %d\n",x,y);
}
 
void tarjan(int x){
    //printf("%d %d\n",x,fa);
    low[x]=dfn[x]=++time;
    stack[++tot]=flag[x]=x;
    //for (int i=1;i<=tot;i++) printf("%d ",stack[i]); printf("\n");
    for (int p=last[x];p;p=pre[p]){
        if (!dfn[other[p]]) tarjan(other[p]),low[x]=min(low[x],low[other[p]]); else
        if (flag[other[p]]) low[x]=min(low[x],dfn[other[p]]);
    }
    if (low[x]==dfn[x]){
        int cur=-;
        while (cur!=x){
            cur=stack[tot--];
            flag[cur]=;
            col[cur]=size;
            num[size]++;
        }
        size++;
    }
}
 
void dfs(int x){
    int cur=;
    for (int p=last[x];p;p=pre[p]){
        if (!len[other[p]]) dfs(other[p]);
        cur=max(cur,len[other[p]]);
    }
    len[x]=cur+num[x];
    //printf(" %d %d\n",x,len[x]);
}
 
void work(int x){
    for (int p=last[x];p;p=pre[p])
        if (len[other[p]]+num[x]==len[x]) {
            if (flag[other[p]]) continue;
            if (!ans[other[p]]) work(other[p]);
            ans[x]+=ans[other[p]];
            stack[++tot]=other[p]; flag[other[p]]=; father[other[p]]=x;
    }
    if (!ans[x]) ans[x]=;
    ans[x]%=d39;
    while (father[stack[tot]]==x) flag[stack[tot--]]=;
}
 
int main(){
    int x,y;
    scanf("%d%d%d",&n,&m,&d39); size=n+;
    for (int i=;i<=m;i++) scanf("%d%d",&x,&y),connect(x,y);
    for (int i=;i<=n;i++) if (!low[i]) tarjan(i);
    //for (int i=1;i<=n;i++) printf("%d %d %d\n",col[i],low[i],dfn[i]);
    for (int i=;i<=n;i++)
        for (int p=last[i];p;p=pre[p])
            if (col[i]!=col[other[p]]) connect(col[i],col[other[p]]),in[col[other[p]]]++;
    //for (int i=n+1;i<size;i++) printf("|%d %d\n",i,num[i]);
    for (int i=n+;i<size;i++) if (!in[i]) dfs(i);
    //for (int i=n+1;i<size;i++) printf("|%d %d\n",i,len[i]);
    for (int i=n+;i<size;i++) p1=max(p1,len[i]);
    for (int i=n+;i<size;i++) if (len[i]==p1) work(i);
    for (int i=n+;i<size;i++) if (len[i]==p1) (p2+=ans[i])%=d39;
    //for (int i=n+1;i<size;i++) printf("|%d %d\n",i,ans[i]);
    printf("%d\n%d\n",p1,p2);
    return ;
}

bzoj 1093 缩点+DP的更多相关文章

  1. BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)

    题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...

  2. bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...

  3. BZOJ 1093 [ZJOI2007]最大半连通子图

    1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1986  Solved: 802[Submit][St ...

  4. POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3241   Accep ...

  5. [bzoj 1093][ZJOI2007]最大半联通子图(强联通缩点+DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1093 分析: 首先肯定是先把强联通全部缩成一个点,然后成了一个DAG 下面要知道一点: ...

  6. BZOJ 1093 强连通缩点+DAG拓扑DP

    缩点后在一个DAG上求最长点权链 和方案数 注意转移条件和转移状态 if (nowmaxn[x] > nowmaxn[v]) { ans[v] = ans[x]; nowmaxn[v] = no ...

  7. BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )

    WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...

  8. bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 先缩点,然后就是找最长链,DP一下即可: 注意缩点后的重边!会导致重复计算答案. 代码 ...

  9. bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)

    1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2286  Solved: 897[Submit][St ...

随机推荐

  1. 201621044079 week13 网络

    作业13-网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以 ...

  2. chrome扩展程序中以编程方式插入内容脚本不生效的问题

    chrome扩展程序中内容脚本有两种插入方式:(https://crxdoc-zh.appspot.com/extensions/content_scripts) 1. 清单文件: 这种方式会在打开每 ...

  3. netbeans调试配置

    apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...

  4. hibernate映射表

    <?xml version="1.0"?>   <!DOCTYPE hibernate-mapping PUBLIC        "-//Hibern ...

  5. Swift & Unicode

    Swift & Unicode emoji let == const https://www.runoob.com/swift/swift-basic-syntax.html

  6. html5 js canvas中画星星的函数

    function drawStar(cxt, x, y, outerR, innerR, rot) { cxt.beginPath(); ; i < ; i++) { cxt.lineTo(Ma ...

  7. 【bzoj4579】[Usaco2016 Open]Closing the Farm 并查集

    题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...

  8. python深浅copy-转自EVA的博客

    感谢Eva_J, http://www.cnblogs.com/Eva-J/p/5534037.html,新手上路,转载纯为自己学习. 初学编程的小伙伴都会对于深浅拷贝的用法有些疑问,今天我们就结合p ...

  9. 后缀数组SA学习笔记

    什么是后缀数组 后缀数组\(sa[i]\)表示字符串中字典序排名为\(i\)的后缀位置 \(rk[i]\)表示字符串中第\(i\)个后缀的字典序排名 举个例子: ababa a b a b a rk: ...

  10. Linux相关——画图软件安装

    其实也不知道算不算Linux相关了... 装个画图软件还是很方便的,刚刚试了一下kolourpaint,感觉还行,就记录下来吧. 先记录几个快捷键emmmm print ---->全屏截图 al ...