bzoj 1093 缩点+DP
首先比较明显的是如果存在一个半连通子图,我们将其中的环缩成点,那么该图仍为半连通子图,这样我们就可以先将整张图缩点,重新构图,新图为拓扑图,记录每个新的点表示的强连通分量中点的个数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的更多相关文章
- BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)
题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...
- bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...
- BZOJ 1093 [ZJOI2007]最大半连通子图
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1986 Solved: 802[Submit][St ...
- POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
- [bzoj 1093][ZJOI2007]最大半联通子图(强联通缩点+DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1093 分析: 首先肯定是先把强联通全部缩成一个点,然后成了一个DAG 下面要知道一点: ...
- BZOJ 1093 强连通缩点+DAG拓扑DP
缩点后在一个DAG上求最长点权链 和方案数 注意转移条件和转移状态 if (nowmaxn[x] > nowmaxn[v]) { ans[v] = ans[x]; nowmaxn[v] = no ...
- BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )
WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...
- bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 先缩点,然后就是找最长链,DP一下即可: 注意缩点后的重边!会导致重复计算答案. 代码 ...
- bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2286 Solved: 897[Submit][St ...
随机推荐
- 我们在删除SQL Sever某个数据库表中数据的时候,希望ID重新从1开始,而不是紧跟着最后一个ID开始需要的命令
一.如果数据重要,请先备份数据 二.删除表中数据 SQL: Delete From ('表名') 如:Delete From abcd 三.执行新语句 SQL: dbcc checkident('表 ...
- P2587 [ZJOI2008]泡泡堂
题目描述 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵双方的教练向组 ...
- FTP-成型版本
1. 旧知识回顾-反射 hasattr(object, name) 说明:判断对象object是否包含名为name的属性(方法) 测试代码如下: class tt(object): def __ini ...
- zlog使用手册
zlog使用手册 来源 http://hardysimpson.github.io/zlog/UsersGuide-CN.html Contents Chapter 1 zlog是什么? 1.1 ...
- 【以前的空间】vijos 1720 阿狸的打字机
https://www.vijos.org/p/1720 作为一个一个蒟蒻,跪了三个星期,终于在蔡大神的帮助下a了.这题网上的题解很多,不过大都把题解写的太简单了(对因为大神的题解只有三个字:傻叉题) ...
- POJ2079:Triangle——题解
http://poj.org/problem?id=2079 题目大意:求最大面积的三角形. —————————————————— 可以知道,最大面积的三角形的顶点一定是最大凸包的顶点. 接下来就是O ...
- BZOJ3076 & 洛谷3081:[USACO2013 MAR]Hill Walk 山走——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3076 https://www.luogu.org/problemnew/show/P3081#sub ...
- Spring多个数据源问题:DataSourceAutoConfiguration required a single bean, but * were found
原因: @EnableAutoConfiguration 这个注解会把配置文件号中的数据源全部都自动注入,不会默认注入一个,当使用其他数据源时再调用另外的数据源. 解决方法: 1.注释掉这个注解 2. ...
- jQuery获取元素对象本身的html
Jquery获取html标签,包含该标签本身 2018年04月01日 20:16:10 清--水 阅读数:829 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...
- UVA10766:Organising the Organisation(生成树计数)
Organising the Organisation 题目链接:https://vjudge.net/problem/UVA-10766 Description: I am the chief of ...