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

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20191   Accepted: 8193

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is  popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 
 
【题解】:
    

有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1,但是如果2也欢迎3那么1也欢迎3.
给出N,M和M个欢迎关系,求被所有牛都欢迎的牛的数量。
用强联通分量做,求连通分量我用的是tarjan算法。
首先求出联通分量的个数,然后依次求各个联通分量的出度,如果仅有一个连通分量出度为0则这个联通分量内的点的个数就是答案;如果有多于一个的联通分量的出度为0,则说明此有向图肯定不连通。因此直接输出0。
 
缩点的意思就是说把求得的强连通分量的点集看成一个点
 
 
【code】:
 /**
Judge Status:Accepted Memory:2404K
Time:532MS Language:G++
Code Length:1971B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm> #define N 10010
using namespace std; vector<int> G[N];
stack<int> stk;
int pre[N],lowlink[N],sccno[N],scc_cnt,dfn_clock,out[N],counter[N]; void DFN(int u) //tarjan算法
{
lowlink[u] = pre[u] = ++dfn_clock;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
DFN(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++; //强连通图的个数标记
while()
{
int x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void findscc(int n)
{
int i;
scc_cnt = dfn_clock = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
for(i=;i<=n;i++)
if(!pre[i])
DFN(i);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
int i;
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b); // 得到图
}
findscc(n); //查找强连通图
int j;
memset(out,,sizeof(out));
memset(counter,,sizeof(counter)); for(i=;i<=n;i++) //遍历一边图,查找统计个点缩点后的出度
{
// cout<<sccno[i]<<" ";
for(j=;j<G[i].size();j++)
{
int v = G[i][j];
if(sccno[i]!=sccno[v])
{
out[sccno[i]]++; //出度
}
}
} for(i=;i<=n;i++)
{
counter[sccno[i]]++; //统计各个强连通分量中的节点个数
} int cnt =,ans = ;
for(i=;i<=scc_cnt;i++)
{
if(!out[i]) //出度为0的强连通分量
{
cnt++;
ans = counter[i]; //答案即为其中的点集数
}
} if(cnt==) printf("%d\n",ans);
else puts(""); return ;
}

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

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

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

  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. 转:Nginx+ffmpeg的HLS开源服务器搭建配置及开发详解

    转:http://itindex.net/detail/51186-nginx-ffmpeg-hls 本文概述: 至目前为止,HLS 是移动平台上非常重要并十分流行的流媒体传输协议.做移动平台的流媒体 ...

  2. c#语法笔记

    书写代码需要注意的地方: 1.代码中出现的所有标点都是英文半角 shift键快速切换中文半角和英文半角 shift+空格 切换全角/半角 2.在c#代码中,每行代码的结束,我们都以分号结束,注意:这个 ...

  3. 《跨终端Web》读书笔记

    跨终端的Web成为了趋势,而这本书就是讲了在这种趋势下进行开发的常见问题及其解决方案,可能是限于篇幅,每个方面都没有展开细说,但这是这样让本书干货满满,几乎没有一句废话. 下面是一些笔记. Web的本 ...

  4. 在虚拟机中安装Linux

    安装CentOS 6.4教程(详细步骤) CentOS是RHEL的克隆版本,功能上是一模一样的,另外重新编译之后还修复了一些后者的bug.主要区别就是CentOS免费,但没有官方的技术支持,而RHEL ...

  5. ComboBoxEdit数据绑定

    ComboBoxEdit也是DevExpress winform控件中经常使用的一个,我们在使用的过程中可能有时需要对ComboBoxEdit控件进行数据绑定,而ComboBoxEdit控件不像 Lo ...

  6. Cocos开发中性能优化工具介绍之Xcode中Instruments工具使用

    Instruments是动态分析工具,它与Xcode集成在一起,可以在Xcode中通过菜单Product→Profile启动.启动如图所示,Instruments有很多跟踪模板可以动态分析和跟踪内存. ...

  7. Entity Framework 之三层架构

    今天,我们谈一下如何用Entity Framework构建一个三层架构.即包括DAL层,BLL层和MODEL层.我们先看一下目录结构,如下图: 目录中,我们有Web层,AVON.DMS.Model是实 ...

  8. 高性能CSS(四)

    移除无匹配的样式 移除无匹配的样式,有两个好处: 第一,删除无用的样式后可以缩减样式文件的体积,加快资源下载速度: 第二,对于浏览器而言,所有的样式规则的都会被解析后索引起来,即使是当前页面无匹配的规 ...

  9. 引用、return

    C语言中没有引用,引用(reference)是c++对c语言的重要扩充.通俗点说,引用就是“起别名”.比如变量data,和它的引用 RefData.虽然名字不同,但是操作他们的时候,都操作的是相同的内 ...

  10. C语言 SDK编程之通用控件的使用--ListView

    一.ListView控件属于通用控件CONTROL中的一种,在SDK编程方式时要使用通用控件 必须包含comctl32.dll,所以代码中要有头文件: commctrl.h 导入库:comctl32. ...