poj 3180 The Cow Prom(强连通分量 Tarjan)
来源:互联网 发布:百度人工智能研究院 编辑:程序博客网 时间:2024/04/29 06:35
题意:有N个牛,围在一水池边,它们用绳子互相绑着(有方向的)。如果绳子的方向一致,它们就能顺时针转,问有多少组牛可以跳舞。
思路:简单有向图的强连通分量。求出强连通分量,且强连通分量里的点数大于等于2的块就能跳舞。
//836K 79MS#include#include#includeusing namespace std;const int VM = 10005;const int EM = 50005;struct Edge{ int to,nxt;}edge[EM];int head[VM],vis[VM],dfn[VM],low[VM];int stack[VM+10],belong[VM];int scc,cnt,top,ep;void addedge (int cu,int cv){ edge[ep].to = cv; edge[ep].nxt = head[cu]; head[cu] = ep++;}int min (int a ,int b){ return a > b ? b : a;}void Tarjan(int u){ dfn[u] = low[u] = ++cnt; vis[u] = 1; stack[top++] = u; int v; for (int i = head[u];i != -1;i = edge[i].nxt) { v = edge[i].to; if (!dfn[v]) { Tarjan(v); low[u] = min(low[u],low[v]); } else if (vis[v]) low[u] = min(low[u],dfn[v]); } if (dfn[u] == low[u]) { ++scc; do{ v = stack[--top]; vis[v] = 0; belong[v] = scc; }while (u != v); }}void solve(int n){ memset (vis,0,sizeof(vis)); memset (dfn,0,sizeof(dfn)); scc = cnt = top = 0; int u,v; for (u = 1;u <= n;u ++) if (!dfn[u]) Tarjan(u); sort (belong+1,belong+n+1); belong[n+1] = -1; // for (int i = 0;i <= n;i ++) // printf ("%d ",belong[i]); int pre = belong[1]; cnt = 0; int ans = 0; for (int i = 1;i <= n+1;i ++) { if (pre == belong[i]) ++cnt; else { pre = belong[i]; if (cnt >= 2) ans ++; cnt = 1; } } printf ("%d\n",ans);}int main (){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif int n,m,u,v; while (~scanf ("%d%d",&n,&m)) { memset (head,-1,sizeof(head)); ep = 0; while (m --) { scanf ("%d%d",&u,&v); addedge (u,v); } solve(n); } return 0;}
- poj 3180 The Cow Prom(强连通分量 Tarjan)
- poj 3180 The Cow Prom 强连通分量tarjan
- Poj 3180 The Cow Prom(强连通分量)
- [POJ 3180] The Cow Prom Tarjan求强连通分量 模板题
- poj 3180 The Cow Prom 强连通分量
- poj 3180 The Cow Prom 强连通分量
- POJ 3180 The Cow Prom【强连通分量分解】
- POJ 3180 The Cow Prom 强连通分量
- POJ 3180 The Cow Prom 求强连通分量 .
- POJ 3180 The Cow Prom 强连通分量
- 强连通分量 POJ 3180 The Cow Prom题解
- POJ 3180 The Cow Prom(Tarjan Algorithm求强连通子集)
- 【POJ】3180 The Cow Prom 强连通
- POJ 3180 The Cow Prom(强连通)
- Poj 3180 The Cow Prom【求强连通分量中元素个数大于1的个数】
- poj3180 The Cow Prom(强连通)
- bzoj1654[The Cow Prom 奶牛舞会] tarjan求有向无环图的强联通分量
- |BZOJ 1654|强连通分量|[Usaco2006 Jan]The Cow Prom 奶牛舞会
- Python3的bytes/str之别
- python3获取天气预报
- python3给图片添加水印
- 新浪微博API OAuth1 Python3客户端
- python3和python2的区别
- poj 3180 The Cow Prom(强连通分量 Tarjan)
- 使用python移动飞信模块发送短信
- Ubuntu 遇到的问题
- python3实现万年历(包括公历、农历、节气、节日)
- python实现DES加密算法和3DES加密算法
- 软件测试框架——自动化测试框架
- python3操作mysql教程
- ATS项目更新(4) 更新DLL到远程服务器
- scrapy技术进阶-URL路径依赖