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 ...
随机推荐
- sublime c++ builder
rt, mark { "cmd": ["g++", "${file}", "-o", "${file_path ...
- cxlibw-5-0.dll was not found
However every once in a while we are getting the following error message: "This application has ...
- C#热血传奇游戏服务端再次开源更新
2014年新春佳节即将到来,也算是送给大家的一份新年礼物.虽然这礼物貌似不给力啊哈哈.(没有用心啊 o(∩_∩)o 哈哈) 这次开源主要去掉上一次开源版本中大量指针代码,简化上手操作,并重构大部分代码 ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- 冒泡排序BubbleSort
/** * * @author Administrator * 功能:交换式排序之冒泡排序 */ package com.test1; import java.util.Calendar; publi ...
- bzoj列表3
水题列表 bzoj2429 裸的最小生成树 bzoj1567 二分答案+hash判断,判断序列.矩阵是否相同常用hash bzoj1087 简单的状压dp bzoj1754 高精度乘法,模拟竖式即可
- BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2049 给出一个森林,起始互不相连,现在有link和cut两种操作,问x,y是否在一棵树里. 分 ...
- ASP.NET MVC3细嚼慢咽---(2)模板页
这一节演示下MVC3中怎样使用模板页,在传统的webform设计模式中,我们使用masterpage作为模板页,在MVC3 razor视图设计中,我们使用另一种方式作为模板页. 新建一个MVC3项目, ...
- Android ViewPager多页面滑动切换以及动画效果
一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了, ...
- ThreadLocal实现方式&使用介绍---无锁化线程封闭
虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以然,因此,使用ThreadLo ...