[ZJOI2007]最大半连通子图

题目大意:

一个有向图称为半连通的,当且仅当对于任意两点\(u,v\),都满足\(u\)能到达\(v\)或者\(v\)能到达\(u\)。

给定一个\(n(n\le10^5)\)个点,\(m(m\le10^6)\)条边的有向图,

问该图最大半连通子图的节点个数及方案数。

思路:

缩点后在DAG上DP求带点权最长链,并统计方案数即可。

源代码:

#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,M=1e6;
std::pair<int,int> edge[M];
std::vector<int> e[N];
std::stack<int> s;
bool ins[N];
int n,m,mod,dfn[N],low[N],scc[N],ind[N],f[N],g[N],size[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
}
void tarjan(const int &x) {
s.push(x);
ins[x]=true;
low[x]=dfn[x]=++dfn[0];
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(!dfn[y]) {
tarjan(y);
low[x]=std::min(low[x],low[y]);
} else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x]) {
scc[0]++;
for(register int y=0;y!=x;s.pop()) {
ins[y=s.top()]=false;
size[scc[y]=scc[0]]++;
}
}
}
std::queue<int> q;
void kahn() {
for(register int i=1;i<=scc[0];i++) {
if(ind[i]==0) {
q.push(i);
f[i]=size[i];
g[i]=1;
}
}
while(!q.empty()) {
const int &x=q.front();
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(f[x]+size[y]>f[y]) {
f[y]=f[x]+size[y];
g[y]=0;
}
if(f[y]==f[x]+size[y]) (g[y]+=g[x])%=mod;
if(--ind[y]==0) q.push(y);
}
q.pop();
}
}
int main() {
n=getint(),m=getint(),mod=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
edge[i]=std::make_pair(u,v);
add_edge(u,v);
}
for(register int i=1;i<=n;i++) {
if(!dfn[i]) tarjan(i);
}
for(register int i=1;i<=n;i++) {
e[i].clear();
}
for(register int i=0;i<m;i++) {
edge[i].first=scc[edge[i].first];
edge[i].second=scc[edge[i].second];
}
std::sort(&edge[0],&edge[m]);
m=std::unique(&edge[0],&edge[m])-edge;
for(register int i=0;i<m;i++) {
if(edge[i].first==edge[i].second) continue;
add_edge(edge[i].first,edge[i].second);
ind[edge[i].second]++;
}
kahn();
int ans=0,cnt=0;
for(register int i=1;i<=scc[0];i++) {
if(f[i]>ans) {
ans=f[i];
cnt=0;
}
if(f[i]==ans) (cnt+=g[i])%=mod;
}
printf("%d\n%d\n",ans,cnt);
return 0;
}

[ZJOI2007]最大半连通子图的更多相关文章

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

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

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

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

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

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

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

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

  5. 洛谷 P2272 [ZJOI2007]最大半连通子图 解题报告

    P2272 [ZJOI2007]最大半连通子图 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v \in V\),满 ...

  6. 题解 P2272 【[ZJOI2007]最大半连通子图】

    P2272 [ZJOI2007]最大半连通子图 萌新初学Tarjan,在<信息学奥赛一本通-提高篇>中看到这题,看到题解不多,便想发布一篇较为清新简洁的题解.--第5道紫题 题目大意: 定 ...

  7. [ZJOI2007]最大半连通子图(Tarjan,拓扑序DP)

    [ZJOI2007]最大半连通子图 题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v ...

  8. Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)

    P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...

  9. BZOJ1093 [ZJOI2007]最大半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

随机推荐

  1. 实现字符串检索strstr函数、字符串长度strlen函数、字符串拷贝strcpy函数

    #include <stdio.h> #include <stdlib.h> #include <string.h> /* _Check_return_ _Ret_ ...

  2. IDEA常见错误

    1. inspects a maven model for resolution problems 在添加Maven依赖的时候,报了inspects a maven model for resolut ...

  3. c语言学习笔记.预处理.#ifndef

    #ifndef -> if not define 配合 #endif使用 在h头文件中使用,防止重复包含和编译. 也可以用条件编译来实现. 例如: 编写头文件 test.h 在头文件开头写上两行 ...

  4. Android中注册获取验证码倒计时按钮

    public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...

  5. 初识PDO数据库抽象层

    目录: 00x1 php中的pdo是什么? 00x2 pdo创建一个PDO对象 00x1 php中的pdo是什么? 就是操作数据库的方法,pdo就是把操作数据库的函数封装成一个pdo类,其间做了安全验 ...

  6. Linux实用命令之git-svn

    近日发现了有一个工具,git-svn,可以打通git svn之间的鸿沟. 很适合习惯于git,却需要维护svn代码的同学. 安装 sudo apt-get install git-svn 具体使用就不 ...

  7. C++学习之路(九):从菱形继承引入的对象模型

    一.单继承 class A {int a;}; class B : public A {int b;}; 普通的单继承关系,类的大小是由其虚表指针和非静态成员函数大小决定.故上述sizeof(A)的大 ...

  8. 斐讯路由器L(联)B(壁)K-码兑换包安全下车通道(图文教程)

    大家好,最近大家比较关心的斐讯路由器如何下车问题,楼主亲自试提取了一遍,记录下过程,欢迎大家一起讨论. 言归正传,上图,上图! No.1 打开斐讯提供的良心k码退换通道: https://tech-s ...

  9. Linux硬链接和软连接的区别与总结

    图示软硬链接的区别 有关硬链接的总结 具有相同inode节点号的多个文件互为硬链接文件: 删除硬链接文件或者删除源文件任意之一,文件实体并未被删除: 只有删除了源文件和所有对应的硬链接文件,文件实体才 ...

  10. php琐碎

    1.类中的常量,可以用类来引用: class MyClass() { const SUCCESS ="success"; const FAIL ="fail"; ...