【BZOJ】1093: [ZJOI2007]最大半连通子图(tarjan+拓扑序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1093
两个条件综合起来加上求最大的节点数,那么很明显如果是环一定要缩点。
然后再仔细思考下就是求dag的最长路的数目啦。。。
然后wa了。。。
看了题解。。。噗!第一次注意到缩点后会有重边QAQ。。。于是。。
orz orz
然后思考了下怎么处理重边。。。很简单,每个点bfs时记录一下就行了。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
#define rdm(x,i) for(int i=ihead[x]; i; i=e[i].next) const int N=100005, M=1000005;
int TT, FF[N], LL[N], vis[N], s[N], top, scc, p[N], w[N], MD, ans1, ans2, in[N], front, tail;
struct Gr {
int ihead[N], n, cnt;
struct dat { int next, to; }e[M];
void add(int u, int v) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; ++in[v]; }
void dfs(int x) {
FF[x]=LL[x]=++TT; vis[x]=1; s[++top]=x;
rdm(x, i) {
int y=e[i].to;
if(!FF[y]) {
dfs(y);
LL[x]=min(LL[x], LL[y]);
}
else if(vis[y]) LL[x]=min(LL[x], FF[y]);
}
if(FF[x]==LL[x]) {
++scc;
int y;
do {
y=s[top--];
vis[y]=0;
p[y]=scc;
w[scc]++;
} while(x!=y);
}
}
void tarjan() {
for1(i, 1, n) if(!FF[i]) dfs(i);
}
void bfs() {
int *d=FF, *f=LL, *q=s;
for1(i, 1, n) d[i]=f[i]=vis[i]=0;
front=tail=0;
for1(i, 1, n) if(!in[i]) q[tail++]=i, d[i]=w[i], f[i]=1;
while(front!=tail) {
int u=q[front++]; if(front==N) front=0;
rdm(u, i) {
int v=e[i].to;
if(!(--in[v])) { q[tail++]=v; if(tail==N) tail=0; }
if(vis[v]) continue;
if(d[v]==d[u]+w[v]) {
f[v]=(f[v]+f[u])%MD;
}
else if(d[v]<d[u]+w[v]) {
d[v]=d[u]+w[v];
f[v]=f[u];
}
vis[v]=1;
}
rdm(u, i) vis[e[i].to]=0;
}
ans1=-1;
for1(i, 1, n) ans1=max(ans1, d[i]);
for1(i, 1, n) if(ans1==d[i]) ans2=(ans2+f[i])%MD;
}
}G, g;
void build() {
for1(i, 1, g.n) in[i]=0;
G.n=scc;
for1(x, 1, g.n) for(int i=g.ihead[x]; i; i=g.e[i].next) if(p[x]!=p[g.e[i].to]) G.add(p[x], p[g.e[i].to]);
} int main() {
read(g.n);
int m=getint(); read(MD);
for1(i, 1, m) { int u=getint(), v=getint(); g.add(u, v); }
g.tarjan();
build();
G.bfs();
printf("%d\n%d\n", ans1, ans2);
return 0;
}
Description

Input
第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述。接下来M行,每行两个正整数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。
Output
应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.
Sample Input
1 2
2 1
1 3
2 4
5 6
6 4
Sample Output
3
HINT
对于100%的数据, N ≤100000, M ≤1000000;对于100%的数据, X ≤10^8。
Source
【BZOJ】1093: [ZJOI2007]最大半连通子图(tarjan+拓扑序)的更多相关文章
- BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )
WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...
- BZOJ 1093 [ZJOI2007]最大半连通子图 - Tarjan 缩点
Description 定义一个半联通图为 : 对任意的两个点$u, v$,都有存在一条路径从$u$到$v$, 或从$v$到$u$. 给出一个有向图, 要求出节点最多的半联通子图, 并求出方案数. ...
- BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)
题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...
- BZOJ 1093 [ZJOI2007]最大半连通子图
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1986 Solved: 802[Submit][St ...
- bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2286 Solved: 897[Submit][St ...
- 【刷题】BZOJ 1093 [ZJOI2007]最大半连通子图
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到 ...
- 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp
题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...
- bzoj 1093: [ZJOI2007]最大半连通子图【tarjan+拓扑排序+dp】
先tarjan缩成DAG,然后答案就变成了最长链,dp的同时计数即可 就是题面太唬人了,没反应过来 #include<iostream> #include<cstdio> #i ...
- bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
随机推荐
- Python list删除元素
pop()方法 pop(n) 从list删除元素Paul同学刚来几天又要转走了,那么我们怎么把Paul 从现有的list中删除呢?如果Paul同学排在最后一个,我们可以用list的pop()方法删除: ...
- Linux 监测常用的图形工具
cacti zabbix nagios nagiosgraph
- 〖Android〗巧用/system/etc/mkshrc文件,把busybox常用命令映射(链接)出来;
在/system/etc/mkshrc文中尾部添加以下代码即可: # for busybox for n in $(busybox --list) do eval alias $n=\'busybox ...
- 2、JSP脚本
JSP脚本 JSP脚本包含了JSP表达式.声明标识和脚本程序.通过这些标识,在JSP页面中可以如同编写Java程序一样来声明变量.定义方法和执行各种表达式的运算 1.在JSP中应用代码片段 语法格式: ...
- JavaScript 对象与数组参考大全
http://www.cnblogs.com/meil/archive/2006/06/28/437527.html本文列举了各种JavaScript对象与数组,同时包括对上述每一对象或数组所完成工作 ...
- Java数据结构和算法(三):常用排序算法与经典题型
常用的八种排序算法 1.直接插入排序 我们经常会到这样一类排序问题:把新的数据插入到已经排好的数据列中.将第一个数和第二个数排序,然后构成一个有序序列将第三个数插入进去,构成一个新的有序序列.对第四个 ...
- C++ string long double转char*
long long q = 10; ]; char* output; sprintf(s, "%ld", q); output = s; double ]; sprintf(s1, ...
- iOS第三方开源库的吐槽和备忘
转自:http://blog.ibireme.com/2013/09/23/ios-third-party-libs/#more-41361 由 ibireme 发表于 2013/09/23 做iOS ...
- HTTP协议 - 协议格式
HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/1.0 ...
- struts2异常处理机制
一.处理一般异常(javaBean异常) struts2进行异常处理首先需要添加exception拦截器,而默认拦截器栈已经加入了这个拦截器,所以不用特意的声明.在Struts 2框架中,采用声明式异 ...