bzoj1093【ZJOI2007】最大半连通子图
2016-02-08 21:37
453 查看
1093: [ZJOI2007]最大半连通子图
Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2368 Solved: 926
[Submit][Status][Discuss]
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
6 6 200706031 2
2 1
1 3
2 4
5 6
6 4
Sample Output
33
HINT
对于100%的数据, N ≤100000, M ≤1000000;对于100%的数据, X ≤10^8。Source
先用Tarjan缩点,这样图就变成了一个DAG。再拓扑排序+DP随便搞一搞就可以了。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #include<map> #include<queue> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define ll long long #define pa pair<int,int> #define maxn 100005 #define maxm 1000005 using namespace std; struct edge{int next,to;}e[maxm],ee[maxm]; int n,m,p,mx,ans,cnt,num,top,tot; int d[maxn],f[maxn],g[maxn],s[maxn],hd[maxn],st[maxn],sz[maxn]; int dfn[maxn],low[maxn],scc[maxn],head[maxn]; map<pa,bool> mp; queue<int> q; inline int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void dfs(int x) { int tmp=0; low[x]=dfn[x]=++tot; st[++top]=x; for(int i=head[x];i;i=e[i].next) { int y=e[i].to; if (!dfn[y]) { dfs(y); low[x]=min(low[x],low[y]); } else if (!scc[y]) low[x]=min(low[x],dfn[y]); } if (low[x]==dfn[x]) { num++; while (tmp!=x) { tmp=st[top--]; scc[tmp]=num; sz[num]++; } } } inline void rebuild() { F(i,1,n) for(int j=head[i];j;j=e[j].next) { int x=scc[i],y=scc[e[j].to]; if (x!=y&&!mp[make_pair(x,y)]) { d[y]++; ee[++cnt]=(edge){hd[x],y}; hd[x]=cnt; mp[make_pair(x,y)]=true; } } } int main() { n=read();m=read();p=read(); F(i,1,m) { int x=read(),y=read(); e[i]=(edge){head[x],y}; head[x]=i; } F(i,1,n) if (!dfn[i]) dfs(i); rebuild(); F(i,1,num) if (!d[i]) { q.push(i); g[i]=sz[i]; s[i]=1; } tot=0; while (!q.empty()) { int tmp=q.front();q.pop(); f[++tot]=tmp; for(int i=hd[tmp];i;i=ee[i].next) { int y=ee[i].to; d[y]--; if (!d[y]) q.push(y); } } F(i,1,num) { int x=f[i]; for(int j=hd[x];j;j=ee[j].next) { int y=ee[j].to; g[y]=max(g[y],g[x]+sz[y]); } } F(i,1,num) { int x=f[i]; for(int j=hd[x];j;j=ee[j].next) { int y=ee[j].to; if (g[y]==g[x]+sz[y]) (s[y]+=s[x])%=p; } } F(i,1,num) mx=max(mx,g[i]); F(i,1,num) if (g[i]==mx) (ans+=s[i])%=p; printf("%d\n%d\n",mx,ans); }
相关文章推荐
- jQuery对象和dom对象区别和转化
- Photoshop脚本编程简介
- 【BZOJ1367】sequence
- 16年年初一记事
- BestCoder Round #71 (div.2) 1004 KK's Number
- POJ 1064_Cable master
- POJ 1064_Cable master
- java基础练习 特殊的数字
- BestCoder Round #71 (div.2) 1002KK's Point
- Android - 设置adb的usb连接配置
- python requests模块的安装和使用方法
- poj 3744 Scout YYF I 矩阵快速幂 概率论
- vxWorks互斥信号量示例
- Web Service Jaxws注解、CXF注解和发布
- 数据结构:线性表
- DNSPod各个套餐的DNS地址
- 第一章:一个简单的Web服务器
- 一个问题:关于类型转换Type Cast(汇编讲解 as 语法)
- Send push notification on Apple (APNS) on c#.net
- Runtime_消息转发机制