题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1051

题解:

强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问到所有的点。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; vector<int> G[maxn],G2[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
int ind[maxn],siz[maxn],vis[maxn];
stack<int> S; int n,m; void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if (!sccno[v]) {
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if (lowlink[u] == pre[u]) {
scc_cnt++;
int cnt = ;
for (;;) {
cnt++;
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x == u) break;
}
siz[scc_cnt] = cnt;
}
} void find_scc(int n) {
dfs_clock = scc_cnt = ;
memset(sccno, , sizeof(sccno));
memset(pre, , sizeof(pre));
for (int i = ; i < n; i++) if (!pre[i]) dfs(i);
//for (int i = 0; i < n; i++) printf("sccno[%d]:%d\n", i, sccno[i]);
} void dfs2(int u) {
if (vis[u]) return;
vis[u] = ;
for (int i = ; i < G2[u].size(); i++) {
int v = G2[u][i];
dfs2(v);
}
} void init() {
for (int i = ; i < n; i++) G[i].clear(), G2[i].clear();
memset(ind, ,sizeof(ind));
memset(vis, , sizeof(vis));
} int main() {
while (scanf("%d%d", &n,&m) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v); u--, v--;
G[u].push_back(v);
}
find_scc(n);
for (int i = ; i < n; i++) {
for (int j = ; j < G[i].size(); j++) {
int v = G[i][j];
if (sccno[i] != sccno[v]) {
G2[sccno[v]].push_back(sccno[i]);
ind[sccno[i]]++;
}
}
}
int rt = -;
for (int i = ; i <= scc_cnt; i++) {
if (ind[i] == ) rt = i;
}
dfs2(rt);
int su = ;
for (int i = ; i <= scc_cnt; i++) {
if (!vis[i]) {
su = ;
break;
}
}
if (su) {
printf("%d\n", siz[rt]);
}
else {
printf("0\n");
}
}
return ;
}

BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点的更多相关文章

  1. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2092  Solved: 1096[Submit][Sta ...

  2. bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...

  3. BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )

    tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解 ----------------------------------------------------------- ...

  4. BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8172  Solved: 4470[Submit][Sta ...

  5. BZOJ 1051: [HAOI2006]受欢迎的牛 缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  6. 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  7. BZOJ 1051: [HAOI2006]受欢迎的牛

    Description 一个有向图,求所以能被别的点到达的点的个数. Sol Tarjan + 强连通分量 + 缩点. 缩点以后找强连通分量,缩点,然后当图有且仅有1个出度为1的点时,有答案. Cod ...

  8. bzoj 1051 [HAOI2006]受欢迎的牛(tarjan缩点)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1051 题解:缩点之后判断出度为0的有几个,只有一个那么输出那个强连通块的点数,否者 ...

  9. 【BZOJ 1051】 1051: [HAOI2006]受欢迎的牛 (SCC)

    1051: [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如 ...

随机推荐

  1. 可以伸缩的查询面板 (searchBar)

    最近有这样的需求,一个页面查询条件特别多,一次全部展示出来的话就占用大量的空间,所以分成了两类,简单搜索和高级搜索,当点击高级搜索的时候就会全部显示. 这样就存在一个问题,页面(navTab,dial ...

  2. OpenGl从零开始之坐标变换

    http://www.tuicool.com/articles/uiayYrI OpenGL学习脚印: 坐标变换过程(vertex transformation) http://blog.csdn.n ...

  3. Dicom格式文件解析器

    转自:http://www.cnblogs.com/assassinx/archive/2013/01/09/dicomViewer.html Dicom全称是医学数字图像与通讯,这里讲的暂不涉及通讯 ...

  4. 'hibernate.dialect' must be set when no Connection avalable

    'hibernate.dialect' must be set when no Connection avalable 当连接不可用时,必须要设置Hibernate方言 搜索大多数的解决办法是:首先查 ...

  5. DropDownList另一种写法

    2013-09-29 17:04:47 1.性别: <asp:DropDownList ID="DrpSex" runat ="server"  Widt ...

  6. gcc常用命令介绍

    GCC 全称是 GNU C Compiler,是gnu中最流行的c & c++编译器,下面我们看一下一些主要的参数使用方法. 对于一个源文件可以直接生成可执行文件 gcc test.c 默认生 ...

  7. C++使用类型代替枚举量

    自己写的C++类型枚举量,可以使用类型识别取代模板,绑定枚举量和多组调用函数,在调用阶段只要指定某组函数就可以根据枚举量调用相应函数. 代码如下: #ifndef __MYENUM_H__ #defi ...

  8. jacob 给word加印的功能

    花了两天时间,参考了一些资料,总算是处理好了这样一个技术点. 关键的心得如下: 使用jacob,重点不是jacob本身,而是office的一些API资料.比如需要知道光标的移动, 包括上下左右的mov ...

  9. linux 编译内核[scripts/kconfig/dochecklxdialog] 错误

    administrator@ubuntu:~/linux-2.6.28-omap$ make menuconfig *** Unable to find the ncurses libraries o ...

  10. ajax对一些没有接口的数据进行分析和添加方法

    对于一些没有接口的数据进行分析和添加方法: <script src="ajax.js"><script>//插入ajax文件 <script> ...