Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 40234   Accepted: 16388

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. 

Source

题意:强连通分量缩点图求出度为0的点。

思路:首先图要连通,其次出度为零的强连通分量个数只能为1.

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
int n, m;
int cnt, num, id;
int head[N];
bool ins[N];
int out[N];
int dfn[N], low[N];
int beg[N];
stack<int> s;
struct P {int to, nxt;} e[N]; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
} void tarjan(int u) {
low[u] = dfn[u] = ++id;
ins[u] = ;
s.push(u);
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (ins[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
int v;
do {
v = s.top();
s.pop();
ins[v] = ;
beg[v] = num;//缩点
} while (u != v);
num++;
}
} int fa[N];
bool vis[N]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void unio(int x, int y) {
int xx = find(x), yy = find(y);
if (xx != yy) fa[xx] = yy;
}
void init() {
memset(head, -, sizeof(head));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
memset(out, , sizeof(out));
memset(beg, , sizeof(beg));
memset(vis,, sizeof(vis));
for (int i = ; i <= n; i++) fa[i] = i;
cnt = num = id = ;
}
int main() {
while (scanf("%d%d", &n, &m) == ) {
init();
for (int i = ; i < m; i++) {
int x, y;
ci(x), ci(y);
add(x, y);
unio(x, y);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = ; i <= n; i++) {
for (int j = head[i]; ~j; j = e[j].nxt) {
int v = e[j].to;
if (beg[i] != beg[v]) out[beg[i]]++;
}
}
int ok = ;
int x = find();
for (int i = ; i <= n; i++)//联通
if (find(i) != x) {
ok = ;
break;
}
int tmp = , cnt = ;
for (int i = ; i <=n; i++) {//强连通分量个数
if (!out[beg[i]]){
if(!vis[beg[i]]) vis[beg[i]]=,cnt++;
tmp++;
}
}
if (cnt==&&ok==) pi(tmp);
else puts("");
}
return ;
}
 

POJ2186 强连通分量+缩点的更多相关文章

  1. POJ2186 (强连通分量缩点后出度为0的分量内点个数)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27820   Accepted: 11208 De ...

  2. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  3. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

  4. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

  5. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  6. ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)

    题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...

  7. POJ2553 The Bottom of a Graph(强连通分量+缩点)

    题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...

  8. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  9. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

随机推荐

  1. 爬虫入门之urllib库(一)

    1 爬虫概述 (1)互联网爬虫 一个程序,根据Url进行爬取网页,获取有用信息 (2)核心任务 爬取网页 解析数据 难点 :爬虫和反爬虫之间的博弈 (3)爬虫语言 php 多进程和多线程支持不好 ja ...

  2. 【Leetcode】【Easy】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. Golang Gin 项目使用 Swagger

    Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...

  4. Vue2自定义指令改变DOM值后未刷新data中绑定属性的值

    标签(空格分隔): Vue 自定义指令用于过滤输入框,只允许输入数字: Vue.directive('numberOnly', { bind: function (el, binding) { el. ...

  5. 二、 OSI模型的实现TCP 、IP

    主要名词定义: IPIP层接收由更低层(网络接口层例如以太网设备驱动程序)发来的数据包,并把该数据包发送到更高层---TCP或UDP层:相反,IP层也把从TCP或UDP层接收来的数据包传送到更低层.I ...

  6. hdu-4288 Coder---线段树+离线处理&离散化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4288 题目大意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3 ...

  7. HDU 2647 拓扑排序

    题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...

  8. 使用maven创建项目

    http://192.168.4.112/rdmanager/main/index.jhtml 1.对于第一次下载某个项目的源码,按照下面的步骤进行: (1)在D:\projects\目录下的空白位置 ...

  9. 解决问题的思路(如故事版里面有东西,却不见了)(swift里面开发比较多)

    解决问题的思路(如故事版里面有东西,却不见了) 正确效果图: 真机效果图: 内容:不见了 解决步骤:(重点讲解方法1) 1.把背景图隐藏了,如果能出现内容,说明背景图把内容遮住了.那怎么办呢,背景图是 ...

  10. 将hexo放到github仓库上

    完成了hexo的安装后, 我们只能在本地访问. 因此我们将它放到github上, 方便我们随时随地的用网址访问. 在Blog文件夹目录下输入: cnpm install --save hexo-dep ...