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 ...
随机推荐
- 【软件分享】文本对比工具 Beyond Compare
转载自公众号:EmbeddDeveloper 对嵌入式感兴趣可以关注原作者博客: http://blog.csdn.net/ybhuangfugui 此处转载为分享用 Ⅰ.摘要 Beyond Comp ...
- WIN32api总结
1.鼠标操作: win32api.SetCursorPos((101,156)) win32api.mouse_event(win32con.MOUSEEVENT_LEFTDOWN,0,0,0,0) ...
- 转载 -- iOS数据持久化存储
作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方 ...
- EWS小记
前段时间和同事完成了一个Exchange 2010 OWA的改造版,他狠狠的把网易邮箱抄了一把,而我则狠狠的被EWS坑了一把.今天打开项目粗略看了一下,发现很多东西都有点记不起来了,思细极恐,决定还是 ...
- OpenVPN下载、安装、配置及使用详解
OpenVPN下载.安装.配置及使用详解 OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可 ...
- 浏览器九宫格的简单实现 - 蒋宇捷的专栏 - 博客频道 - CSDN.NET
CSS3 来源:http://blog.csdn.net/hfahe/article/details/6125890#1536434-hi-1-22083-42d97150898b1af15ddaae ...
- java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.
java的类必须大写 java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致. 在dos用上下箭头,调用已用过的命令
- VS2005代码自动提示功能失灵
http://bbs.csdn.net/topics/340036305 方法很简单:把整个项目复制一份(文件夹名与原来不同就行).打开项目(此时可以注意到状态条显示正在更新intelligence) ...
- plsql exist和in 的区别
<![endif]--> <![endif]--> 发现公司同事很喜欢用exists 和in 做子查询关联,我觉得很有必要研究下 两者的区别,供参考和备忘 /* (这段信息来自 ...
- IPv6 tutorial – Part 8: Special addresses
https://4sysops.com/archives/ipv6-tutorial-part-8-special-addresses/ The special IPv6 addresses disc ...