POJ2186 Popular Cows 强连通分量
来源:互联网 发布:js点击按钮打开新窗口 编辑:程序博客网 时间:2024/05/17 04:18
题目链接:http://poj.org/problem?id=2186
今天复习一下图论强连通,到poj上找到裸题,刷着玩一下,顺便水篇博客。
大概就是强连通缩一下点,然后我们看一下,缩点后的点如果有多个出度为0,则不存在,否则的话唯一一个出度为零的即为所求。
#include <cstdio>#include <algorithm>#include <stack>using namespace std;stack <int> dl;const int MAXN = 150000;int head[MAXN],to[MAXN],nxt[MAXN],dfn[MAXN],low[MAXN],ins[MAXN],sg[MAXN];int oud[MAXN],sum[MAXN];int cnt,n,m,a,b,tot,nfg,tjs;void ad_edg(int x,int y){nxt[++tjs] = head[x];head[x] = tjs;to[tjs] = y;}void sread(){scanf("%d%d",&n,&m);for (int i = 1;i <= m;i++){scanf("%d%d",&a,&b);if (a == b) continue;ad_edg(a,b);}}void tarjan(int x){dfn[x] = low[x] = ++cnt;dl.push(x),ins[x] = 1;for (int i = head[x];i;i = nxt[i]){if (!dfn[to[i]]){tarjan(to[i]);low[x] = min(low[x],low[to[i]]);}else if (ins[to[i]])low[x] = min(low[x],dfn[to[i]]);}if (low[x] == dfn[x]){sg[x] = ++tot,sum[tot]++;while (dl.top() != x) ins[dl.top()] = 0,sg[dl.top()] = tot,dl.pop(),sum[tot]++;ins[x] = 0,dl.pop();}}void swork(){for (int i = 1;i <= n;i++)if (!dfn[i]) tarjan(i);for (int i = 1;i <= n;i++)for (int j = head[i];j;j = nxt[j])if (sg[i] != sg[to[j]])oud[sg[i]]++;cnt = 0,a = 0;for (int i = 1;i <= tot;i++)if (!oud[i])cnt++,a = i;if(cnt > 1 || !cnt) printf("0\n");elseprintf("%d\n",sum[a]);}int main(){sread();swork();return 0;}
0 0
- POJ2186 Popular Cows【Kosaraju】【强连通分量】
- POJ2186 Popular Cows【Tarjan】【强连通分量】
- poj2186 Popular Cows(强连通分量tarjan)
- POJ2186 Popular Cows 强连通分量
- POJ2186 Popular Cows(强连通分量)
- POJ2186 Popular Cows 强连通分量
- POJ2186 Popular Cows 【图论】【强连通分量】
- poj2186 Popular Cows(强连通分量)
- poj2186 Popular Cows 有向图 强连通分量
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
- poj2186 Popular Cows(强连通分量)(korasaju||tarjan模板题)
- poj2186 Popular Cows--Kosaraju算法 & 缩点 & 强连通分量
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】
- 图论:POJ2186-Popular Cows (求强连通分量)
- poj2186 Popular Cows --- 强连通
- poj2186 Popular Cows【强连通】
- POJ2186 Popular Cows 强连通 Tarjan
- POJ2186 Popular Cows 【裸缩环+Gabow算法强连通分量分解再建图】
- 2017我的个人目标
- Python应用中使用MongoDB方法步骤详解
- 83. Remove Duplicates from Sorted List
- 【退烧——终极追求】韧锋 Realforce静电容 87U 十周年 静音版键盘
- 关于asp下拉绑定传值问题
- POJ2186 Popular Cows 强连通分量
- 我想问下关于android app问题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 【js类库AngularJs】解决angular+springmvc的post提交问题
- 安卓5.0获取当前应用的栈顶Activity
- Android Studio精彩案例(六)《使用一个Demo涵盖补间动画所有知识》
- C++对象指针
- 1 - c_call_Python api
- Java开发—环境搭建之Eclipse中安装Maven
- MySQL · 答疑解惑 · MySQL 锁问题最佳实践