poj2186 Popular Cows(强连通)
崇拜有传递性。求所有牛都崇拜的牛
tarjan算法求强连通。
如果不连通就不存在。如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std; const int N = 10005;
const int M = 100005; struct Edge {
int to, next;
} edge[M];
int head[N];
int cnt_edge; void add_edge(int u, int v)
{
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
head[u] = cnt_edge;
cnt_edge++;
} int dfn[N];
int low[N];
int stk[N];
bool in[N];
int kind[N]; int top;
int idx;
int cnt; int n, m; void dfs(int u)
{
dfn[u] = low[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!dfn[v])
{
dfs(v);
low[u] = min(low[v], low[u]);
}
else if(in[v])
{
low[u] = min(low[u], dfn[v]);
}
} if (low[u] == dfn[u])
{
++cnt;
int j;
do {
j = stk[top--];
in[j] = false;
kind[j] = cnt;
} while (j != u);
}
} void init()
{
memset(dfn, 0, sizeof dfn);
memset(head, -1, sizeof head);
cnt_edge = 0;
top = cnt = idx = 0;
} int deg[N]; void solve()
{
// 缩点后出度为0的点个数为1
for (int u = 1; u <= n; ++u)
{
int k = kind[u];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (kind[u] != kind[v]) deg[k]++;
}
} int flag = 0;
int p;
for (int i = 1; i <= cnt; ++i)
{
if (deg[i] == 0)
{
flag++;
p = i;
if (flag > 1) break;
}
}
if (flag == 1)
{
int ans = 0;
for (int i = 1; i <= n; ++i) if (kind[i] == p) ++ans;
printf("%d\n", ans);
}
else
{
printf("0\n");
}
} int main()
{
while (~scanf("%d%d", &n, &m))
{
if (n == 0 && m == 0) break;
int a, b;
init();
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &a, &b);
add_edge(a, b);
} for (int i = 1; i <= n; ++i)
{
if (!dfn[i]) dfs(i);
} solve();
}
return 0;
}
poj2186 Popular Cows(强连通)的更多相关文章
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- POJ2186 Popular Cows 强连通分量tarjan
做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...
- poj2186 Popular Cows --- 强连通
给一个有向图,问有多少结点是其它全部结点都能够到达的. 等价于,在一个有向无环图上,找出度为0 的结点.假设出度为0的结点仅仅有一个,那么这个就是答案.假设大于1个.则答案是0. 这题有环.所以先缩点 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- 【Tarjan缩点】POJ2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35644 Accepted: 14532 De ...
- poj2186 Popular Cows 题解——S.B.S.
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29642 Accepted: 11996 De ...
随机推荐
- 【jsp+jpa】Check your ViewResolver setup!
困扰了好几天的坑 javax.servlet.ServletException: Circular view path [fileupload]: would dispatch back to the ...
- Convert.ToString和ToString的区别
Convert.ToString能处理字符串为null的情况,不抛出异常. ToString方法不能处理字符串为null的情况,会抛出异常.如:“未将对象引用设置到对象的实例”.
- 最近在折腾VPS(持续完善)
买的某国内vps. 本机环境 Win7-x64 使用官方英文版的putty,用于远程登录linux主机.WinSCP图形界面管理文件. VPS CentOS 6.2 安装 安全狗(safedog.cn ...
- MS提供的虚拟机IE测试
https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/
- 正确使用c语言中的头文件
我们在使用c编程的时候经常会遇到头文件,前段时间我自己做了个小项目的时候,也遇到了关于头文件的问题. 预处理器发现#include 指令后,就会寻找后跟的文件名并把这个文件包含的内容包含到当前文件中. ...
- Eclipse使用技巧及个性化设计
以下除特殊说明均在 Windows->Preferences里面操作 如何把Eclipse关闭提示调出来? General->Startup and Shutdown,在 Confirm ...
- -fembed-bitcode is not supported on versions of iOS prior to 6.0 问题修复
参考一下网址 http://stackoverflow.com/questions/30867544/fembed-bitcode-is-not-supported-on-versions-of-io ...
- Understanding Memory Management(2)
Understanding Memory Management Memory management is the process of allocating new objects and remov ...
- cmd find命令
find 作用:从文件中收索字符串 格式:find 参数 "字符串" 路径\文件名 参数: /V 显示所有未包含指定字符串的行. /C 仅显示包含字符串的行数. /N 显示行号. ...
- 使用 Azure Site Recovery 将内部部署虚拟化工作负荷迁移至 Azure
ABHISHEK A. HEMRAJANI 云 + Enterprise 项目经理 我们最近发布了微软的灾难恢复即服务 (DRaaS) 产品的预览版 - Azure SiteRecovery.此产 ...