BZOJ 1051 [HAOI2006]受欢迎的牛
来源:互联网 发布:拉尼娅王后知乎 编辑:程序博客网 时间:2024/06/01 09:05
题目描述 传送门
学习了Tarjan缩点。
#include<cstdio>#include<iostream>#include<cstring>#include<vector>#include<algorithm>#include<stack>using namespace std;const int maxn=10005;vector<int> g[maxn];int dfs_clock,scc_cnt,sccno[maxn],pre[maxn],lowlink[maxn],cd[maxn],scctot[maxn];stack<int>s;void dfs(int u){ pre[u]=lowlink[u]=++dfs_clock; s.push(u); for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(!pre[v]){ dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if(!sccno[v]){ lowlink[u]=min(lowlink[u],pre[v]); } } if(lowlink[u]==pre[u]){ scc_cnt++; for(;;){ int x=s.top();s.pop(); sccno[x]=scc_cnt; if(x==u) break; } }}void find_scc(int n){ dfs_clock=scc_cnt=0; memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); for(int i=1;i<=n;i++) if(!pre[i]) dfs(i);}int main(){ int n,m; cin>>n>>m; for(int i=0;i<m;i++){ int a,b; scanf("%d%d",&a,&b); g[a].push_back(b); } find_scc(n); memset(scctot,0,sizeof(scctot)); memset(cd,0,sizeof(cd)); for(int i=1;i<=n;i++){ scctot[sccno[i]]++; for(int j=0;j<g[i].size();j++) if(sccno[g[i][j]]!=sccno[i]) cd[sccno[i]]++; } int ans=233333,tot=0; for(int i=1;i<=scc_cnt;i++) if(cd[i]==0) ans=scctot[i],tot++; if(tot==1) printf("%d\n",ans); else puts("0"); return 0;}
阅读全文
0 0
- bzoj 1051: [HAOI2006]受欢迎的牛
- BZOJ 1051 [HAOI2006]受欢迎的牛
- [BZOJ]1051: [HAOI2006]受欢迎的牛
- 【bzoj 1051】[HAOI2006]受欢迎的牛
- bzoj 1051: [HAOI2006]受欢迎的牛
- BZOJ 1051 [HAOI2006]受欢迎的牛 tarjan
- [bzoj-1051] [HAOI2006]受欢迎的牛
- BZOJ 1051 [HAOI2006]受欢迎的牛
- BZOJ 1051 [HAOI2006]受欢迎的牛
- bzoj 1051 [HAOI2006]受欢迎的牛
- bzoj 1051 [HAOI2006]受欢迎的牛
- bzoj 1051[HAOI2006]受欢迎的牛|tarjan|水题
- 【连通分量】BZOJ 1051:[HAOI2006]受欢迎的牛
- bzoj 1051: [HAOI2006]受欢迎的牛(tarjan 缩点)
- 【bzoj 1051】[HAOI2006]受欢迎的牛(Tarjan缩点)
- 【bzoj 1051】 [HAOI2006]受欢迎的牛 强联通分量
- [BZOJ]1051: [HAOI2006]受欢迎的牛 强连通
- 【BZOJ】1051 [HAOI2006]受欢迎的牛 强联通分量
- 高二&高一&初三模拟赛27 总结
- ORA-01541: system tablespace cannot be brought offline; shut down if necessary
- 单线程,多线程,线程池方法大文件指定单词数量统计
- 数据库基本命令
- Android LockSupport 分析
- BZOJ 1051 [HAOI2006]受欢迎的牛
- K-means++算法思想
- TP5上传图片Call to a member function move() on null
- GoldenGate(4)_问题答疑与部署中的错误记录
- SSL2776 2017年10月19日提高组 整除(容斥+dfs)
- C++实现string类
- bzoj 3173: [Tjoi2013]最长上升子序列
- JavaScript综述
- 2017-10-19-Mysql 常用函数