id=2186">【2186】Popular Cows(强联通分支及其缩点)

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28323   Accepted: 11459

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

题目大意是有n头牛,他们有m对特殊的关系

关系A B表示A敬仰B 恩 是感觉怪怪的= =

然后假设A敬仰B B敬仰C 那么A也敬仰C 也就是关系是可传递的

要求找出被其它牛都敬仰的牛的数目

一開始非常费解。要不是放在强连通里,实在没法往这方向靠……

事实上反过来想的话比較好像,强连通分支的定义——强连通分支中从不论什么一个点都能够訪问到其余各点(有向图)

这样再回到题中 能够得出两个结论

1.假设某个强联通分支中的某仅仅牛被分支外的全部牛都敬仰,也就是说分支外的牛都有一条通向他的路,和他在同一个强连通分支里的全部牛也是满足要求的。

2.假设分支中有某头牛敬仰分支外的牛。那么就不存在被全部牛都敬仰的牛。(能够换种方式来想,假设分支中某头牛敬仰分支外的牛,还被其余牛都敬仰,那么分支外的这头牛也应该被包括在分支内。由于这样就说明分支外的那头牛。会有一条通向分支内的路径,也就是符合强连通分量的定义)

3.经2结论可知,出度为0的强连通分支。就是满足条件的牛群。

但假设有两群。就不存在这样的牛。由于两个分支间是没有关系的。

能够自己画一画看看。

找到方法推断就好办了,首先把全部的强连通分支求出来,缩点后变成一团团的。找出出度为0的缩点。假设存在两个或两个以上,答案就是0。假设之存在一个,那么这个点中的全部牛都是满足题意的牛,统计输出就可以。

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#define LL long long
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const double eps = 1e-8; struct Edge
{
int v,next;
}; Edge eg[50050];
int head[10010];
int vis[10010];
//dfs序
int dfn[10010];
//最早可訪问到的点
int low[10010];
//缩点的出度
int in[10010];
//缩点是否满足出度为0
bool can[10010];
//缩点的根
int pre[10010];
int n,tp,tm;
stack <int> s; void tarjan(int u)
{
s.push(u);
vis[u] = 1;
low[u] = dfn[u] = tm++;
int v; for(int i = head[u]; i != -1; i = eg[i].next)
{
v = eg[i].v;
//点未被訪问过
if(vis[v] == 0)
{
tarjan(v);
vis[v] = 1;
low[u] = min(low[u],low[v]);
}
//点在栈中
else if(vis[v] == 1)
{
low[u] = min(low[u],dfn[v]);
}
} //此点为树根
if(low[u] == dfn[u])
{
int x = s.top();
while(x != u)
{
pre[x] = u;
s.pop();
x = s.top();
}
//标记该分支中各点树根为u
pre[x] = u;
s.pop();
}
} int main()
{
int u,v;
while(~scanf("%d%d",&n,&tp))
{
memset(head,-1,sizeof(head)); for(int i = 0; i < tp; ++i)
{
scanf("%d%d",&u,&v);
eg[i].v = v;
eg[i].next = head[u];
head[u] = i;
} memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; ++i)
{
if(vis[i]) continue; tm = 0;
tarjan(i);
} memset(in,0,sizeof(in));
memset(can,0,sizeof(can));
int f = 0;
for(int i = 1; i <= n; ++i)
{
for(int j = head[i]; j != -1; j = eg[j].next)
{
v = eg[j].v;
//两个点不在同一个分支内
if(pre[v] != pre[i])
{
in[i]++;
}
}
//该点出度不为0
if(in[i])
{
//该缩点出度不为0
can[pre[i]] = 1;
}
} int cnt = 0;
for(int i = 1; i <= n; ++i)
{
//该点为分支根,同一时候该分支出度为0
if(pre[i] == i && can[i] == 0)
{
f = i;
cnt++;
}
if(cnt > 1) break;
} //出度为0的分支多余1个
if(cnt != 1)
{
puts("0");
}
else
{
cnt = 0;
//统计分支中的点数
for(int i = 1; i <= n; ++i)
{
if(pre[i] == f) cnt++;
}
printf("%d\n",cnt);
}
} return 0;
}

【2186】Popular Cows(强连通分支及其缩点)的更多相关文章

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

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

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

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

  4. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  5. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  6. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  7. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  8. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

  9. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  10. POJ-2186 Popular Cows,tarjan缩点找出度为0的点。

    Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...

随机推荐

  1. battery-获取手机电量信息

    我们如果想要获得手机的电池电量信息,可以借助广播来实现.因为当手机电池电量发生变化的时候,系统会发送一个广播.具体代码如下 //注册 intentFilter.addAction(Intent.ACT ...

  2. Objective-C基础笔记(8)Foundation常用类NSString

    一.创建字符串的方法 void stringCreate(){ //方法1 NSString *str1 = @"A String!"; //方法2 NSString *str2 ...

  3. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  4. DTU(用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备)

    DTU (Data Transfer unit),是专门用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备.DTU广泛应用于气象.水文水利.地质等行业.

  5. 【习题 7-4 UVA-818】Cutting Chains

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二进制枚举要解开哪些环. 把所有和它相关的边都删掉. 对于剩下的联通分量. 看看是不是每一个联通分量都是一条链 ->每个点的度 ...

  6. Android 内存监测工具

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/幻影浪子 [博主导读]俗话说:工欲善其事必先利其器!我们先来了解下内存监测工具是怎么使用的?为内 ...

  7. 全球可信并且唯一免费的HTTPS(SSL)证书颁发机构:StartSSL

    全球可信并且唯一免费的HTTPS(SSL)证书颁发机构:StartSSL http://blog.s135.com/startssl/ 购买权威机构的证书一年大概得七八千元,其实这是不值得的,所以一直 ...

  8. .condarc(conda 配置文件)

    Configuration - Conda documentation .condarc以点开头,一般表示 conda 应用程序的配置文件,在用户的家目录(windows:C:\\users\\use ...

  9. vue使用(二)

    本节目标:           1.数据路径的三种方式          2.{{}}和v-html的区别 1.绑定图片的路径 方法一:直接写路径 <img src="http://p ...

  10. GO语言学习(四)GO语言语言结构

    Go Hello World 实例 Go 语言的基础组成有以下几个部分: 包声明 引入包 函数 变量 语句 & 表达式 注释 接下来让我们来看下简单的代码,该代码输出了"Hello ...