博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2186 Proving Equivalences targan+缩点
阅读量:3903 次
发布时间:2019-05-23

本文共 1914 字,大约阅读时间需要 6 分钟。

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 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.

 题意:

给出仰慕关系, 求有几个奶牛被除了自己的所有奶牛仰慕。

看着题解才勉强看懂。 。

先对给出的关系运用targan算法+缩点将强连通分量进行缩点。 。

如果缩点为1个, 则说明这n个奶牛相互崇拜。 。。

然后求他们的出度。

如果有大于1个的点出度为0, 说明所有的奶牛都不符合条件。 。为神魔呢, 因为出度大于1的话, 说明至少有一只奶牛谁也不崇拜, 所以不满足条件。

如果只有一个点出度为1, 则说明这个缩点的所有点都符合条件, 只要求出此缩点有多少个点即可。 。

代码如下:

 

#include 
#include
#include
#include
#include
#include
using namespace std;const int maxn=10005;vector
ve[maxn];int n,m;int dfn[maxn],low[maxn],ccnc[maxn],on[maxn];int tot,ccnum;stack
s;void init (){ ccnum=tot=0; memset (dfn,0,sizeof(dfn)); memset (low,0,sizeof(low)); memset (ccnc,0,sizeof(ccnc)); memset (on,0,sizeof(on)); for (int i=0;i<=n;i++) ve[i].clear(); while (!s.empty()) s.pop();}void targan (int x){ dfn[x]=low[x]=++tot; s.push(x); for (int i=0;i
1) { printf("0\n"); return; } for (int i=1;i<=n;i++) if(ccnc[i]==loc) ans++; printf("%d\n",ans);}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { init(); while (m--) { int x,y; scanf("%d%d",&x,&y); ve[x].push_back(y); } for (int i=1;i<=n;i++) if(!dfn[i]) targan(i); finds(); } return 0;}

 

转载地址:http://ctaen.baihongyu.com/

你可能感兴趣的文章
Open Judge 4010 :2011
查看>>
百练OJ-2815 城堡问题【DFS】
查看>>
CODE[VS] 1025 选菜 【背包】
查看>>
POJ 1724 ROADS【DFS+剪枝】
查看>>
AOJ 847 整数拆段
查看>>
AOJ 848 分数拆分
查看>>
UVA 133 The Dole Queue 【约瑟夫环】
查看>>
XDOJ 1208 B.笑爷买房 【DFS】
查看>>
部门年度工作总结的内容
查看>>
pandas学习笔记
查看>>
Numpy笔记
查看>>
正则表达式
查看>>
python线程进程笔记
查看>>
TensorFlow初学者必须了解的55个经典案例
查看>>
机器学习笔记
查看>>
数十种TensorFlow实现案例汇集:代码+笔记
查看>>
python记录的错误与知识
查看>>
内核中各种套接字的关系
查看>>
linux sysctl 参数实现 暨 ip_forward参数对Linux内核转发影响分析
查看>>
linux 路由表 的一些相关资料
查看>>