题目链接
NNN个牛,MMM个关系 (A,B)A(A,B)A(A,B)A认为BBB是受欢迎的。求受所有牛欢迎的牛的数量。

思路

同一个强联通分量里面的牛是相互受欢迎的,我们将所有的联通分量求出来之后,那些出度为零的联通分量就是答案,前提保证这样的联通分量只有一个。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <cstring>
#include <time.h>
#define LL long long
#define P pair<int, int>
#define lowbit(x) (x & -x)
#define mem(a, b) memset(a, b, sizeof(a))
#define mid ((l + r) >> 1)
#define lc rt<<1
#define rc rt<<1|1
#define endl '\n'
const int maxn = 1e4 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
using namespace std;
vector<int> g[maxn];
int Stack[maxn], low[maxn], dfn[maxn], inStack[maxn], belong[maxn];
int vis[maxn], color[maxn];
int now, len, cnt;
void init() {now = len = cnt = 0;mem(inStack, 0);mem(belong, 0);mem(dfn, 0);mem(low, 0);
}
void tarjan(int x) {low[x] = dfn[x] = ++now;Stack[++len] = x;inStack[x] = 1;int tmp = g[x].size();for (int i = 0; i < tmp; ++i) {int y = g[x][i];if (!dfn[y]) tarjan(y), low[x] = min(low[x], low[y]);else if (inStack[y]) low[x] = min(low[x], low[y]);}if (dfn[x] == low[x]) {++cnt;int top;while (Stack[len] != x) {top = Stack[len--];belong[top] = cnt;inStack[top] = 0;}top = Stack[len--];belong[top] = cnt;inStack[top] = 0;}
}
int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int n, m;while (cin >> n >> m) {for (int i = 1; i <= n; ++i) {g[i].clear();}for (int i = 0, u,v; i < m; ++i) {cin >> u >> v;g[u].push_back(v);}init();for (int i = 1; i <= n; ++i) {if (dfn[i]) continue;tarjan(i);}mem(vis, 0);for (int i = 1; i <= n; ++i) {for (int j = 0; j < (int)g[i].size(); ++j) {int v = g[i][j];int l = belong[i];int r = belong[v];if (l == r) continue;vis[l] = 1;}}int sum = 0, ans = 0, flag;for (int i = 1; i <= n; ++i) {if (vis[belong[i]] == 0) {vis[belong[i]] = 1;sum++;flag = belong[i];}}for (int i = 1; i <= n; ++i) {if (belong[i] == flag) ans++;}if (sum != 1) ans = 0;cout << ans << endl;}return 0;
}/*
3 3
1 2
2 3
3 13 3
1 2
2 1
2 35 4
1 4
2 4
3 4
5 45 5
1 2
2 3
3 1
1 4
4 55 6
1 2
2 3
3 1
1 4
4 5
5 32 2
1 2
2 13 2
1 2
2 16 6
1 2
2 3
3 1
1 4
4 5
5 35 6
1 2
2 3
3 1
1 4
4 5
5 45 7
4 1
1 2
2 3
3 1
1 4
4 5
5 45 6
1 2
2 3
3 1
1 4
4 5
5 17 9
1 2
2 3
3 1
4 5
5 6
6 4
4 7
7 1
1 76 6
1 2
2 3
3 1
4 5
5 6
6 44 4
1 2
2 3
3 1
1 44 4
1 2
2 3
3 1
4 15 6
1 2
2 3
3 1
5 1
5 4
3 47 9
1 2
2 3
3 1
5 1
5 4
3 4
4 7
7 6
6 43
1
1
1
5
2
0
0
2
5
5
4
0
1
3
1
3
*/

POJ-2186 Popular Cows (Tarjan缩点) 文末有测试数据相关推荐

  1. Poj 2186 Popular Cows(Tarjan 强连通缩点)

    传送门:Poj 2186 题意:给你n头牛,m种关系,A牛认为B牛是popular的,B牛认为C牛是popular的,则A也认为C是popular的,问最终有几头被所有牛认为是popular的牛 题解 ...

  2. pku 2186 Popular Cows (tarjan缩点)

    http://poj.org/problem?id=2186 将所有最大连通分量缩点,然后统计缩点后每个点的出度,出度为0的肯定就是了可是这个点可能是缩出来的,所以要记录这个点真正包含的点数.如果出度 ...

  3. POJ - 2186 Popular Cows(强连通缩点)

    题目链接:点击查看 题目大意:给出n只奶牛,以及m组可传递的关系,每组关系给出一个a和一个b,表示为a->b,意思是奶牛a觉得奶牛b很酷,因为关系可传递,所以如果a->b且b->c, ...

  4. POJ 2186 Popular Cows(强连通分量缩点,Tarjan算法)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16578 [解题报告] 给你一个有向图,问你有多少个点可以被其它 ...

  5. POJ 2186 Popular Cows(Tarjan)

    http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...

  6. POJ 2186 Popular Cows (强联通分量)

    链接 :http://poj.org/problem?id=2186 一个联通分量里的所有的牛满足任何一个被其他牛认为是红人.强联通缩点之后 只需要找到一个且只有一个联通分量且它的出度为0 答案就是这 ...

  7. POJ 2186 Popular Cows

    POJ_2186 这个题目其实就是求完强连通分量之后,判断一下出度为0的强连通分量是否唯一,如果唯一输出该强连通分量的点数,否则输出0. 由于之前求强连通分量的时候是把同一个强连通分量里的数放到一个数 ...

  8. POJ 2186 popular cow 有向图的强联通问题 Tarjan算法

    参考:http://hi.baidu.com/1093782566/blog/item/e5a0e9229913bd048b82a175.html http://www.cppblog.com/Iro ...

  9. POJ 2168 Popular Cows

    在zyh的高中,学生会每一年都会评选受欢迎学生,所谓受欢迎的学生,就是被所有学生喜欢的学生. 每个学生都喜欢自己,碰巧的是,如果学生A 喜欢学生B ,学生B喜欢学生C, 那么学生A也喜欢学生C. 但是 ...

最新文章

  1. 基于图文界面的蓝牙扫描工具btscanner
  2. conda下用prefix创建虚拟环境会怎么样?
  3. 高效的判断素数---筛选法
  4. 计算机组成原理-数制与编码
  5. 基于Ocelot的gRpcHttp网关
  6. 【错误纠正】关于文章《绕开数学,讲讲信息论》
  7. Numpy and Theano broadcasting
  8. 基于观察者模式——创建显示天气数据
  9. JAVA喝咖啡的关系_写完java就去喝咖啡,很合适。
  10. UG 6.0软件安装教程
  11. 冷热分离和直接使用大数据库_【TBase开源版测评】深度测评TBase的shard分片和冷热分离存储特性...
  12. 计算机睡眠和休眠哪个更好,电脑睡眠和休眠哪个好?电脑休眠和睡眠的区别介绍...
  13. [js]调用google,51ditu和mapbar的地图API
  14. GIS地图描边特效的实现
  15. 手机怎么压缩图片?分享一下压缩的好方法
  16. 微信公众号关注渠道来源
  17. 华为和小米:在智能电视市场的边缘疯狂试探
  18. Jenkins系列之——第一章 Jenkins下载及安装
  19. windows安装Oracle12 (服务端+客户端)
  20. HTML几种设置水平居中和垂直居中的方式

热门文章

  1. 如何在线将pdf转换成ppt格式
  2. Git远程分支的回退
  3. mac下在eclipse中怎样清除/切换svn
  4. window安装mysql5.7解压版(解决乱码问题)
  5. 查看oracle数据库的连接数以及用户
  6. nodejs随记04
  7. 循环语句until和while
  8. 查询优化器内核剖析第一篇
  9. PHP学习记录第一篇:Ubuntu14.04下LAMP环境的搭建
  10. 纯CSS美化单复选框(checkbox、radio)