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. 
 
解析:
  如果只有一个连通分量的出度为0  则输出这个连通分量里的点数
  其它则为0
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m;
vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt, out[maxn];
stack<int> S;
int vis[maxn]; 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(pre[v], lowlink[u]);
}
if(lowlink[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} int main()
{
int u, v;
rd(n), rd(m);
for(int i=; i<m; i++)
{
rd(u), rd(v);
G[u].push_back(v);
}
for(int i=; i<=n; i++)
if(!pre[i]) dfs(i);
// cout << 111 << endl;
if(scc_cnt == )
{
pd(n);
return ;
}
for(int i=; i<=n; i++)
for(int j=; j<G[i].size(); j++)
{
int v = G[i][j];
if(sccno[i] != sccno[v])
out[sccno[i]]++;
}
int res = ;
// cout << scc_cnt << endl;
// for(int i=1; i<=scc_cnt; i++)
// cout << in[i] << endl; int cnt = ;
for(int i=; i<=scc_cnt; i++)
if(out[i] == )
cnt++;
if(cnt != )
return puts(""), ;
for(int i=; i<=n; i++)
if(out[sccno[i]] == ) res++; pd(res); return ;
}

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

  1. poj 2186 强连通分量

    poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...

  2. Popular Cows(POJ 2186)

    原题如下: Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40746   Accepted: 16 ...

  3. POJ(2186)强连通分量分解

    #include<cstdio> #include<vector> #include<cstring> using namespace std; ; vector& ...

  4. (连通图 缩点 强联通分支)Popular Cows -- poj --2186

    http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...

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

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

  6. Popular Cows (POJ No.2186)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  7. 图论:POJ2186-Popular Cows (求强连通分量)

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  8. poj 1904(强连通分量+输入输出外挂)

    题目链接:http://poj.org/problem?id=1904 题意:有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国 ...

  9. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

随机推荐

  1. helm 部署 使用 记录

    0.概念:Helm作为一个包管理工具, 它把Kubernetes资源(比如deployments.services或 ingress等) 打包到一个chart中,方便我们将其chart保存到chart ...

  2. abp 取消权限校验

    在abp中,通过ABP_PERMISSIONS表来存储定义appService中的方法权限校验.设置方式如下: [AbpAuthorize(PermissionNames.Pages_Users)] ...

  3. PCB之PASTE助焊层和SOLDER阻焊层

    1.PASTE为焊接层,用于SMT贴片元件的焊接,对应的图形为钢网(钢网上的小孔): 2.SOLDER为阻焊层,它代表的是绿油的涂抹区域,且为负片输出(负片输出指的是图形以外的区域为有效区域): PA ...

  4. POJ Remmarguts' Date

    题目链接-> 题解: 次短路模板. 代码: #include<cstdio> #include<iostream> using namespace std; #defin ...

  5. Scala学习(五)---Scala中的类

    Scala中的类 摘要: 在本篇中,你将会学习如何用Scala实现类.如果你了解Java或C++中的类,你不会觉得这有多难,并且你会很享受Scala更加精简的表示法带来的便利.本篇的要点包括: 1. ...

  6. 面试2——java基础1

    1.int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象 ...

  7. TiDB入门(四):从入门到“跑路”

    前言 前面三章基本把 TiDB 的环境弄好了,也做了一下简单测试,有兴趣的同学可以看一下: TiDB 入门(一):TiDB 简介 TiDB 入门(二):虚拟机搭建 TiDB-Ansible 部署方案 ...

  8. MVC 使用cshtml的一些基础知识-和相关整理

    首先在认识cshtml之前,先要了解一下Razor视图引擎 如果对此有疑问的话可以借鉴 博客园博文:http://kb.cnblogs.com/page/96883/ 或 博客博文:http://ww ...

  9. ARM-GPIO

    操作GPIO有三种方法: 调用库函数读取IO的输入电平:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_t GPIO_pin): 操作寄 ...

  10. Websocket(一)——原理及基本属性和方法

    初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处? 答案很简单,因为 HTTP 协议有一个缺陷:通信只能由客户端发起. 举例 ...