BZOJ 1051 Tarjan
来源:互联网 发布:日本南北朝 知乎 编辑:程序博客网 时间:2024/06/08 19:18
这道题明显是一个图论,告诉了你受欢迎的关系是具有传递性的就意味着可以用图来存。先将问题简单化,如果这种关系是一棵树,那么答案一定是树根。当然如果是森林,答案就是所有的树根。那么我们如何将图转换成树呢?用Tarjan缩点即可。用Tarjan缩点后答案即为所有连通块中入度为0的连通块的个数。
#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespace std;#define maxn 50005int pre[maxn],last[maxn],other[maxn],l;int dfn[maxn],low[maxn],dfs_clock,scc_cnt,scc[maxn];int n,m,in0[maxn],out0[maxn],siz[maxn],ans;stack<int> s; void connect(int x,int y){ l++; pre[l]=last[x]; last[x]=l; other[l]=y; } void dfs(int u){ dfn[u]=low[u]=++dfs_clock; s.push(u); for (int p=last[u];p;p=pre[p]) { int v=other[p]; if (!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if (!scc[v]) low[u]=min(low[u],dfn[v]); } if (dfn[u]==low[u]) { scc_cnt++; while (1) { int x=s.top();s.pop(); scc[x]=scc_cnt; siz[scc_cnt]++; if (x==u) break; } }} int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); connect(a,b); } for (int i=1;i<=n;i++) if (!dfn[i]) dfs(i); for (int u=1;u<=n;u++) for (int p=last[u];p;p=pre[p]) { int v=other[p]; if (scc[u]!=scc[v]) out0[scc[u]]=in0[scc[v]]=1; } for (int i=1;i<=scc_cnt;i++) if (out0[i]==0) ans+=siz[i]; printf("%d\n",ans); return 0;}
0 0
- BZOJ 1051 Tarjan
- bzoj 1051简单tarjan
- 【tarjan】BZOJ 1051:受欢迎的牛
- 【BZOJ 1051】 受欢迎的牛 【Tarjan】
- BZOJ 1051 [HAOI2006]受欢迎的牛 tarjan
- BZOJ 1179 Tarjan + spfa
- BZOJ 2730 Tarjan求割点
- bzoj 1123(tarjan)
- [BZOJ 1051][HAOI 2006]受欢迎的牛(tarjan缩点)
- bzoj 1051[HAOI2006]受欢迎的牛|tarjan|水题
- BZOJ 1051 || POJ 2186 受欢迎的牛 Tarjan
- 【bzoj】1051 &&【poj】2186 Popular Cows Tarjan scc
- bzoj 1051: [HAOI2006]受欢迎的牛(tarjan 缩点)
- 【bzoj 1051】[HAOI2006]受欢迎的牛(Tarjan缩点)
- Tarjan缩点 BZOJ 1051 受欢迎的牛
- BZOJ[1051][HAOI2006]受欢迎的牛 Tarjan缩点
- [Tarjan]BZOJ 1051—— [HAOI2006]受欢迎的牛
- 【tarjan】【bzoj 1123】: [POI2008]BLO
- Spring MVC Fast Tutorial: Model View Controller
- Qt中pro文件的详细介绍
- 玲珑学院-1014-Absolute Defeat【思维】
- Arithmetic problem | 统计前面比自己小的数的个数
- Java并发编程番外篇(二)happens-before关系
- BZOJ 1051 Tarjan
- Oracle笔记002
- getopt(3)
- android studio 启动问题
- AMS-总结
- python之路
- mysql 命令行基础操作
- WEB——Servlet介绍
- Spring MVC 快速教程:Hello World Spring MVC Fast Tutorial: Hello World