http://poj.org/problem?id=2186

题意:

一个有向图,求出点的个数(任意点可达)。

思路:

Kosaraju算法的第一次dfs是后序遍历,而第二次遍历时遍历它的反向图,从标号最大的结点开始遍历。

对于这道题,在求解强连通分量之后,能被所有点可达只可能是最后一个强连通块,根据遍历时的拓扑序,我们可以计算出最后一个的结点个数。

但是我们最后还是要判断一下,这个连通块是不是任意结点可达。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std; const int maxn=+; int n,m;
vector<int> G[maxn];
vector<int> rG[maxn];
vector<int> vs; //后序遍历顺序的顶点列表
int vis[maxn];
int sccno[maxn]; //所属强连通分量的拓扑序
int scc_cnt; //强连通分量数量 void add_edge(int from,int to)
{
G[from].push_back(to);
rG[to].push_back(from);
} void dfs1(int u)
{
if(vis[u]) return;
vis[u]=;
for(int i=;i<G[u].size();i++) dfs1(G[u][i]);
vs.push_back(u);
} void dfs2(int u,int k)
{
if(sccno[u]) return;
sccno[u]=k;
for(int i=;i<rG[u].size();i++) dfs2(rG[u][i],k);
} void find_scc(int n)
{
scc_cnt=;
vs.clear();
memset(sccno,,sizeof(sccno));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++) if(!vis[i]) dfs1(i);
for(int i=n-;i>=;i--)
if(!sccno[vs[i]]) dfs2(vs[i],++scc_cnt);
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
u--; v--;
add_edge(u,v);
}
find_scc(n);
int u=;
int ans=;
//计算出最后一个强连通分量中点的数量
for(int i=;i<n;i++)
{
if(sccno[i]==scc_cnt)
{
u=i;
ans++;
}
} //判断每个点是不是都能可达u
memset(sccno,,sizeof(sccno));
dfs2(u,);
for(int i=;i<n;i++)
{
if(sccno[i]==)
{
ans=;
break;
}
}
printf("%d\n",ans);
}
return ;
}

POJ 2186 Popular Cows(强连通分量Kosaraju)的更多相关文章

  1. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

  3. POJ 2186 Popular Cows 强连通分量模板

    题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  4. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  5. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  6. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  7. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  8. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  9. POJ 2186 Popular cows(Kosaraju+强联通分量模板)

    题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...

随机推荐

  1. Linux之Redis-redis哨兵集群详解

    1.Sentinel 哨兵 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所 ...

  2. Latch free等待事件

    Latch free等待事件的三个参数:p1-latch的地址:p2-latch编号:p3-请求次数.从oracle10g起,latchfree不再包含所有的latch等待,有些latch等待可能表现 ...

  3. Python编程题总结

    1 数学篇 1.1 斐波那契数列 def fib(n): if n in (0, 1): return n fib_n1, fib_n2 = 0, 1 for i in range(2, n + 1) ...

  4. 3.1.7. Cross validation of time series data

    3.1.7. Cross validation of time series data Time series data is characterised by the correlation bet ...

  5. hdu4057 Rescue the Rabbit

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4057 题目: Rescue the Rabbit Time Limit: 20000/10000 MS ( ...

  6. HDU 4746 Mophues(莫比乌斯反演)

    题意:求\(1\leq i \leq N,1\leq j \leq M,gcd(i,j)\)的质因子个于等于p的对数. 分析:加上了对质因子个数的限制. 设\(f(d):[gcd(i,j)=d]\) ...

  7. (译)Windows Azure:移动后端开发的主要更新

    Windows Azure:移动后端开发的主要更新 这周我们给Windows Azure释出了一些很棒的更新,让云上的移动应用开发明显的简单了.这 些新功能包括: 移动服务:定制API支持移动服务:G ...

  8. 一个辅助AWVS C段扫描的小php脚本

    小菜写的小脚本,大牛轻拍砖~~~~~~ 渗透前信息收集时喜欢用椰树来获取旁站及二级域名,然后根据二级.三级域名地址扩展C段,扩大扫描业务边界.例如 以联想为例 但,各个旁站对应IP可能不同,或有CDN ...

  9. sparkSQL——DataFrame&Datasets

    对于新司机,可能看到sc与spark不知道是什么,看图知意 ********************************************************************** ...

  10. 使用selenium前学习HTML(3)——元素

    <!-- HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 注释:开始标签常被称为开放标签(opening tag),结束标签常称为闭合标签(clos ...