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 ...
随机推荐
- VM启动报错:Failed to lock the file
http://www.cnblogs.com/kristain/articles/2491966.html Reason: Failed to lock the fileGoogle 了一下, 在網路 ...
- 开发设计模式(一)Command模式
Command定义 将来自客户端的请求传入一个对象,无需了解这个请求激活的 动作或有关接受这个请求的处理细节. 这是一种两台机器之间通讯联系性质的模式,类似传统过程语 言的 CallBack功能. 优 ...
- 微软职位内部推荐-ATG Engineer II
微软近期Open的职位: ATG Engineer - GeneralistReady to work on some of the most advanced hardware on the pla ...
- webkit私有css3属性 -webkit-overflow-scrolling:touch;
-webkit-overflow-scrolling:touch;/*允许独立的滚动区域和触摸回弹*/ 这个属性可以提高滚动的平滑度
- ExtJS4.2学习(16)制作表单(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-10/188.html --------------- ...
- php数组遍历 使用while循环
while() 通常和 list(),each()配合使用. $colors= array('red','blue','green','yellow'); while(list($key,$val)= ...
- 李洪强漫谈iOS开发[C语言-021]-运算符
- MMU、Icache、Dcache
http://blog.csdn.net/iodoo/article/details/8954014 i-cache(instruction cache)是指令高速缓冲存储器. Cache存储体:存放 ...
- 【HDOJ】5564 Clarke and digits
DP+快速矩阵幂.注意base矩阵的初始化,不难. /* 5564 */ #include <iostream> #include <string> #include < ...
- Covariance and Contravariance in C#, Part One
http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.a ...