题目链接:http://poj.org/problem?id=2186

题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”。所谓的“受欢迎的点”当且仅当任何一个点出发都能到达它。

原来的图是无序且可能有环,用tarjan缩点,变成一个DAG。受欢迎点的出度一定为0,所以缩点后求有多少个连通分量的出度是0,要是大于1则没有受欢迎的点,等于1的话求出这个出度为0的连通分量有多少个点。

强联通分量tarjan算法里邻接表用vector一般好理解,但是速度不及链式前向星,链式前向星学习链接:http://blog.csdn.net/acdreamers/article/details/16902023

AC代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int MAXN = 1e4 + ;
struct data {
int next , to;
}edge[MAXN * ];
int head[MAXN] , low[MAXN] , dfn[MAXN] , block[MAXN] , st[MAXN];
int top , ord , sccnum;
bool instack[MAXN] , out[MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
low[i] = dfn[i] = ;
head[i] = -;
instack[i] = false;
}
top = ord = sccnum = ;
} void tarjan(int u) {
low[u] = dfn[u] = ++ord;
st[++top] = u;
instack[u] = true;
for(int i = head[u] ; ~i ; i = edge[i].next) { //链式前向星
int v = edge[i].to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u] , low[v]);
}
else if(instack[v]) {
low[u] = min(low[u] , dfn[v]);
}
}
if(low[u] == dfn[u]) {
int v;
sccnum++;
do {
v = st[top--];
instack[v] = false;
block[v] = sccnum;
}while(u != v);
}
} int main()
{
int n , m , u , v;
while(~scanf("%d %d" , &n , &m)) {
init(n);
for(int i = ; i < m ; i++) {
scanf("%d %d" , &u , &v);
edge[i].to = v; //链式前向星
edge[i].next = head[u];
head[u] = i;
}
for(int i = ; i <= n ; i++) {
if(!dfn[i])
tarjan(i);
}
memset(out , false , sizeof(out)); //缩点是否有入度
for(int u = ; u <= n ; u++) {
for(int i = head[u] ; ~i ; i = edge[i].next) { //链式前向星
int v = edge[i].to;
if(block[v] != block[u]) //不是同一个连通分量
out[block[u]] = true;
}
}
int res = , op = -;
for(int i = ; i <= sccnum ; i++) {
if(!out[i]) { //出度为0的点
res++;
op = i;
}
}
if(res > ) {
printf("0\n");
}
else {
res = ;
for(int i = ; i <= n ; i++) {
if(block[i] == op)
res++;
}
printf("%d\n" , res);
}
}
}

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

  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(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  5. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

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

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

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

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

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

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

  9. POJ 2186 Popular Cows (强联通)

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

随机推荐

  1. ffmpeg开发指南

    FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用.FFmpeg支持MPEG.DivX.M ...

  2. Liunx系统学习一,liunx系统的目录结构及含义

    LIUNX系统目录结构: “/” ===>这是linux文件系统的入口,也是整个linux文件系统的根目录,linux不同于windows,没有所谓的C,D,E盘,整个liunx只有一个根分区 ...

  3. 也说Autofac在MVC的简单实践:破解在Controller构造函数中的实例化 - winhu

    相信大家对Autofac并不陌生,很多人都在使用.本文只是介绍一下本人在使用时的一点想法总结. 在使用一个框架时,肯定要去它的官网查阅一下.autofac的官网给出了一些经典的使用案例.如注册容器: ...

  4. 云计算服务模型,第 2 部分: 平台即服务(PaaS)

    英文原文:Cloud computing service models, Part 2: Platform as a Service 平台即服务 (PaaS) 常常是最容易让人迷惑的云计算类别,因为很 ...

  5. 实时通讯库 libre/librem/restund/baresip

    http://www.creytiv.com/ 源码下载 libre Toolkit library for asynchronous network IO with protocol stacks ...

  6. HDU 5874 Friends and Enemies

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. 几种常见的FTP软件的二进制设置说明

    几种常见的FTP软件的二进制设置说明: 1.FlashFXP: 打开 FlashFXP:在工具栏中,选项 => 参数(也可以直接按F6键),在弹出来的窗口中,选择“传输(T)”卡,在传输模式中选 ...

  8. Geodesic-based robust blind watermarking method for three-dimensional mesh animation by using mesh segmentation and vertex trajectory

    之前因为考试,中断了实验室的工作,现在结束考试了,不能再荒废了. 最近看了一篇关于序列水印的文章,大体思想是:对于一个网格序列,首先对第一帧进行处理,在第一帧上,用网格分割算法(SDF)将网格分割成几 ...

  9. MVC和WebForm的优缺点对比

    1 WebForm优点 1)支持事件模型开发,得益于丰富的服务端组件,WebForm开发可以迅速的搭建Web应用 2)使用方便,入门容易 3)控件丰富的WebForm 2 WebForm缺点  1)封 ...

  10. ***JAVA多线程的应用场景和应用目的举例

    多线程使用的主要目的在于: 1.吞吐量:你做WEB,容器帮你做了多线程,但是他只能帮你做请求层面的.简单的说,可能就是一个请求一个线程.或多个请求一个线程.如果是单线程,那同时只能处理一个用户的请求. ...