题目链接:

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. markdownpad2使用说明

    ## 欢迎使用 MarkdownPad 2 ## **MarkdownPad** 是 Windows 平台上一个功能完善的 Markdown 编辑器. ### 专为 Markdown 打造 ### 提 ...

  2. ASP.NET MVC3 301永久重定向实现程序

    使用 ASP.NET 又喜欢跟进新技术的朋友可能已经知道,在 ASP.NET 4.0 中增加了 Response.RedirectPermanent() 方法来实现永久重定向,方法的作用在注释中解释的 ...

  3. dropdownlist分页

    <div class="new-paging" id=""> <div class="new-tbl-type"> ...

  4. DFS入门之一

    深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...

  5. c语言 char*类型作为中间变量将许多字符串保存到一个数组的问题

    char*是一个字符串指针,如下面的程序value_作为一个中间变量用来在for循环中scanf输入的值的接收者,然后将value_保存到array中,但是一下程序会出现一个问题就是当你跳出这个函数时 ...

  6. 4.Servlet_Form表单处理

    1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...

  7. winform Config文件操作

    using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Config ...

  8. How to using x++ creating Vendors [AX2012]

    .//Create party for the vendor public void createParty(VendorRequestCreate _vendorRequestCreate) { ; ...

  9. python 安装 setuptools Compression requires the (missing) zlib module 的解决方案

    背景: 虚拟机centos下安装python辅助工具 setuptools报错,错误信息大概如下: Traceback (most recent call last): File "setu ...

  10. DBus接口文档

    gitgit.projects.genivi.org / ipc / common-api-dbus-tools.git / blob? search: re 0544e985b6e4a6c83ddf ...