【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp
题目描述
一个有向图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的余数。
输入
第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤100000, M ≤1000000;对于100%的数据, X ≤10^8
输出
应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.
样例输入
6 6 20070603
1 2
2 1
1 3
2 4
5 6
6 4
样例输出
3
3
题解
Tarjan+拓扑排序+dp
显然,如果原图是一个DAG,那么选择的就是一条链,答案就是最长链;
如果不是呢?Tarjan缩点,然后拓扑排序+dp求带权最长链即可。正确性显然。
需要注意的是缩完点后如果有重边需要只考虑一条的贡献,因为确定了点就确定了边的选择,只有一次转移的机会。
时间复杂度 $O(n+m)$
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cstring>
#define N 100010
using namespace std;
queue<int> q;
vector<int> e[N] , v[N];
int p , deep[N] , low[N] , tot , ins[N] , sta[N] , top , bl[N] , si[N] , num , ind[N] , last[N];
struct data
{
int x , y;
data(int a = 0 , int b = 0) {x = a , y = b;}
data operator+(int a) {return data(x + a , y);}
data operator^(data a)
{
if(x > a.x) return *this;
else if(x < a.x) return a;
else return data(x , (y + a.y) % p);
}
}f[N];
void tarjan(int x)
{
vector<int>::iterator i;
deep[x] = low[x] = ++tot , ins[x] = 1 , sta[++top] = x;
for(i = e[x].begin() ; i != e[x].end() ; i ++ )
{
if(!deep[*i]) tarjan(*i) , low[x] = min(low[x] , low[*i]);
else if(ins[*i]) low[x] = min(low[x] , deep[*i]);
}
if(deep[x] == low[x])
{
int t;
num ++ ;
do
{
t = sta[top -- ] , ins[t] = 0;
bl[t] = num , si[num] ++ ;
}while(t != x);
}
}
void solve(int n)
{
vector<int>::iterator i;
data ans;
int x;
for(x = 1 ; x <= n ; x ++ )
for(i = e[x].begin() ; i != e[x].end() ; i ++ )
if(bl[x] != bl[*i])
v[bl[x]].push_back(bl[*i]) , ind[bl[*i]] ++ ;
for(x = 1 ; x <= num ; x ++ )
if(!ind[x])
f[x] = data(si[x] , 1) , q.push(x);
while(!q.empty())
{
x = q.front() , q.pop() , ans = ans ^ f[x];
for(i = v[x].begin() ; i != v[x].end() ; i ++ )
{
if(last[*i] != x) last[*i] = x , f[*i] = f[*i] ^ (f[x] + si[*i]);
ind[*i] -- ;
if(!ind[*i]) q.push(*i);
}
}
printf("%d\n%d\n" , ans.x , ans.y);
}
inline char nc()
{
static char buf[100000] , *p1 , *p2;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf , 1 , 100000 , stdin) , p1 == p2) ? EOF : *p1 ++ ;
}
inline int read()
{
int ret = 0; char ch = nc();
while(!isdigit(ch)) ch = nc();
while(isdigit(ch)) ret = ((ret + (ret << 2)) << 1) + (ch ^ '0') , ch = nc();
return ret;
}
int main()
{
int n = read() , m = read() , i , x , y;
p = read();
for(i = 1 ; i <= m ; i ++ ) x = read() , y = read() , e[x].push_back(y);
for(i = 1 ; i <= n ; i ++ )
if(!deep[i])
tarjan(i);
solve(n);
return 0;
}
【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的有向路径.若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\) ...
- bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...
- 【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 ...
随机推荐
- Prism for WPF 搭建一个简单的模块化开发框架(二)
原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...
- 【NIS】深入了解NIS
1 简介 NIS( NetworkInformation Service)提供了一个网络黄页的功能,当用户登录系统时,Linux系统会到NIS主机上去寻找用户使用的帐号密码信息加以比对,以提供用户登 ...
- core 中ef 连接sql server数据库 在类库中 自动生成 model
首先 介绍 Scaffold-DbContext "Server=.;database=sdd;User Id=sa;Password=123456;" Microsoft.Ent ...
- Python:PyCharm如何导入模块
- 金山注入浏览器默认开启上网导航 www.uu114.cn
金山注入浏览器默认开启上网导航 www.uu114.cn 今天突然发现我的电脑所有浏览器打开后,都会默认打开一个www.uu114.cn网站,chrome.firefox和IE都中招了.经过排查,发现 ...
- CentOS 7.2安装11g Grid Infrastructure
Preface Oracle claimed that 11g RAC is supported on Redhat Linux 7 and above version,but the ...
- 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)
分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...
- mysql数据库基本操作命令
1.登录命令 mysql -u root -p "password" 2.列出所有数据库 show databases; 3.使用数据库 use db_name 4.列出数据库中所 ...
- Qt-Qml-隐藏标题栏-程序依附任务栏
最近换工作,直接欢动qml这边来了,以后可能会有更多关于qml的文章 今天第一个,qml下面怎么隐藏标题栏 第一种方法是在使用QQuickView加载qml文件的话,这里就可以使用QQuickView ...
- Iview 表单提交: Cannot read property 'validate' of undefined
提交表单时,提示报这个错,找了半天,然后也百度了很久,一直没找到答案,代码如下: <link href="~/lib/iview3.1.4/styles/iview.css" ...