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. 
 
题解:给你M组u,v代表v是受u欢迎的,并且欢迎具有传递性;让你求最多有多少人是相互受欢迎的;
SCC + 缩点;强连通分量跑一边,然后缩点,就可转化为DAG图;然后记录每个 “ 点 ” 的出度;如果有超过一个,那么输出0即可;
如果为一个,则输出其所在点集的大小;
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
#define eps 1e-8
typedef pair<int,int> P;
const int maxn=5e4+;
int N,M,u[maxn],v[maxn],tot,times,blocks;
int head[maxn],dfn[maxn],lowv[maxn];
int ins[maxn],outd[maxn],belong[maxn],sz[maxn];
struct Node{
int v,nxt;
} edge[maxn];
stack<int> st;
void Init()
{
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(lowv,,sizeof lowv);
memset(ins,,sizeof ins);
memset(outd,,sizeof outd);
memset(sz,,sizeof sz);
memset(belong,,sizeof belong);
tot=times=blocks=;
while(!st.empty()) st.pop();
} void Addedge(int u,int v)
{
edge[tot].v=v;
edge[tot].nxt=head[u];
head[u]=tot++;
} void Tarjan(int u)
{
dfn[u]=lowv[u]=++times;
st.push(u);
ins[u]=;
for(int i=head[u];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!dfn[v]) Tarjan(v),lowv[u]=min(lowv[u],lowv[v]);
else if(ins[v]) lowv[u]=min(lowv[u],dfn[v]);
} if(dfn[u]==lowv[u])
{
++blocks;
int v;
do
{
v=st.top(); st.pop();
belong[v]=blocks;
sz[blocks]++;
ins[v]=;
} while(u!=v);
} } int main()
{
ios::sync_with_stdio(false);
cin>>N>>M;
Init();
for(int i=;i<=M;i++)
{
cin>>u[i]>>v[i];
Addedge(u[i],v[i]);
}
int cnt=,flag;
for(int i=;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=M;i++) if(belong[u[i]]!=belong[v[i]]) outd[belong[u[i]]]++;
for(int i=;i<=blocks;i++) if(outd[i]==) cnt++,flag=i; if(cnt!=) cout<<<<endl;
else cout<<sz[flag]<<endl;
return ;
}

POJ 2186 Popular cows(SCC 缩点)的更多相关文章

  1. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

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

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

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

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

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

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

  5. POJ 2186 Popular Cows (强联通)

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

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

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

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

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

  8. POJ 2186 Popular Cows(Targin缩点)

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

  9. poj 2186 Popular Cows

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

随机推荐

  1. sql性能分析(explain关键字)

    explain关键字结果 列名所代表的的含义: Id:MySQL QueryOptimizer 选定的执行计划中查询的序列号.表示查询中执行 select 子句或操作表的顺序,id 值越大优先级越高, ...

  2. 不止面试—jvm类加载面试题详解

    面试题 带着问题学习是最高效的,本次我们将尝试回答以下问题: 什么是类的加载? 哪些情况会触发类的加载? 讲一下JVM加载一个类的过程 什么时候会为变量分配内存? JVM的类加载机制是什么? 双亲委派 ...

  3. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  4. nyoj 458-小光棍数 (471)

    458-小光棍数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:6 提交数:6 难度:1 题目描述: 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111, ...

  5. nyoj 242-计算球体积 (pi*r*r*r*4/3)

    242-计算球体积 内存限制:64MB 时间限制:3000ms 特判: No 通过数:21 提交数:74 难度:1 题目描述: 根据输入的半径值,计算球的体积. 输入描述: 输入数据有多组,每组占一行 ...

  6. CSS(8)---通俗讲解定位(position)

    CSS(8)---通俗讲解定位(position) CSS有三种基本的定位机制: 普通流.浮动.定位.前面两个之前已经讲过,详见博客: 1.CSS(5)---通俗讲解盒子模型 2.CSS(6)---通 ...

  7. django_4:数据库0——配置数据库

    使用Mysql数据库 (python需要能连接上mysql,见别的文档:python3+django 支持 mysql) 启动mysql服务 修改setting.py同目录 下的__init__.py ...

  8. vux组件的样式变量的使用

    使用x-header,查看文档发现有个样式变量,可以改变x-header的样式 这玩意怎么用呢? 1.在项目中创建一个.less样式文件,例如我这里是创建一个src/style/vux_theme.l ...

  9. 扛把子组作业要求 20191024-3 互评Alpha阶段作品

    此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9860] 组名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 韩昊 刘信鹏 ...

  10. 封装Ajax和跨域

    目录 引言 封装ajax 案例:使用自封装ajax 案例:动态加载瀑布流 跨域 引言 对于Ajax现在相信大家已经不会陌生了,无论是原生的XMLHttpRequest方式发送还是通过jQuery框架中 ...