Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29642   Accepted: 11996

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只牛,牛A认为牛B很牛,牛B认为牛C很牛。
给你M个关系(谁认为谁牛),求大家都认为它很牛的牛有几只。
p.s.如果牛A认为牛B很牛,牛B认为牛C很牛。那么我们就认为牛A认为牛C很牛。
 /*
Problem: poj 2186
OJ: POJ
User: S.B.S.
Time: 297 ms
Memory: 1468 kb
Length: 2420 b
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<bitset>
#include<vector>
#include<list>
#include<map>
#define maxn 10001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define maxm 50001
#define mod 998244353
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
struct EDGE
{
int from;
int to;
int next;
}edge[maxm];
int head[maxn];
int dfn[maxn],low[maxn],stack1[maxn];
int num[maxn],du[maxn],vis[maxn];
int tim,tp,dcnt,sum;
int cnt,time=,top,cut,tot;
inline void addedge(int u,int v)
{
edge[tot].from=u;
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot;
tot++;
}
inline void dfs(int u,int fa)
{
dfn[u]=time;
low[u]=time;
time++;
vis[u]=;
stack1[top]=u;
top++;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v]){
dfs(v,u);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
cut++;
while(top>&&stack1[top]!=u)
{
top--;
vis[stack1[top]]=;
num[stack1[top]]=cut;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n>>m;
M(head,-);
F(i,,m-){
int a,b;
cin>>a>>b;
addedge(a,b);
}
F(i,,n) if(!vis[i]) dfs(i,);
F(i,,n)for(int j=head[i];j!=-;j=edge[j].next){
if(num[i]!=num[edge[j].to]){
du[num[i]]++;
}
}
int x;sum=;
F(i,,cut)
if(!du[i]){
sum++;
x=i;
}
if(sum==){
sum=;
F(i,,n) if(num[i]==x) sum++;
cout<<sum<<endl;
}
else cout<<""<<endl;
return ;
}

poj 2186

poj2186 Popular Cows 题解——S.B.S.的更多相关文章

  1. POJ2186 Popular Cows 题解 强连通分量入门题

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  2. POJ2186 Popular Cows 题解 强连通分量

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  3. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  4. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  5. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  6. 【Tarjan缩点】POJ2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35644   Accepted: 14532 De ...

  7. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

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

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

  9. POJ2186:Popular Cows——题解

    http://poj.org/problem?id=2186 题面纯英文--就不粘题面了. 简单的说,就是将图强连通分量缩点,然后拓扑一下. 为了避免拓扑,我们可以反向存图,然后查入度为0的点即可. ...

随机推荐

  1. Java学习(API及Object类、String类、StringBuffer字符串缓冲区)

    一.JAVA的API及Object类 1.API 概念: Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就 ...

  2. 【LOJ】#2017. 「SCOI2016」围棋

    题解 考虑到状态数比较复杂,其实我们需要轮廓线dp-- 我们设置\(f[x][y][S][h][k]\)为考虑到第(x,y)个格子,S是轮廓线上的匹配状态,是二进制,如果一位是1表示这一位匹配第一行匹 ...

  3. shell脚本中${var1:-var2}

    在一个shell脚本中看见一行代码: DATE=${:-`date "+%Y%m%d" -d "-1 day"`} 查了一下 ${var1:-var2} 这种结 ...

  4. 洛谷P2151 [SDOI2009] HH去散步 [矩阵加速]

    题目传送门 HH去散步 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走 ...

  5. [转]SharePoint 2010 Powershell Feature Cmdlets

    In this installment its time to look at the various cmdlets that have to do with Features. Of course ...

  6. 实验吧--隐写术--九连环--WriteUp

    题目: http://ctf5.shiyanbar.com/stega/huan/123456cry.jpg 是一张图: 放到binwalk查看一下 发现存在压缩文件. 使用-e参数将文件分离 打开文 ...

  7. 【BZOJ 2671】 2671: Calc (数论,莫比乌斯反演)

    2671: Calc Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 303  Solved: 157 Description 给出N,统计满足下面条件 ...

  8. 【推导】【贪心】【高精度】Gym - 101194E - Bet

    题意:每个队伍有个赔率pi,如果你往他身上押x元,它赢了,那么你得到x+(1/pi)x元,否则你一分都得不到.问你最多选几支队伍去押,使得存在一种押的方案,不论你押的那几支队伍谁赢,你都能赚得到钱. ...

  9. [NC13B]贝伦卡斯泰露/[51Nod1400]序列分解

    [NC13B]贝伦卡斯泰露/[51Nod1400]序列分解 题目大意: 给定\(A_{1\sim n}(n\le40)\),问是否能将\(A\)分解成两个相同的子序列? 思路: 折半搜索.时间复杂度\ ...

  10. 为什么java的构造方法中this()或者super()要放在第一行

    java的构造方法中如果自己显性的调用super()的时候一定要放在第一行,如不是的话就会报错. 为什么一定要在第一行? super()在第一行的原因就是: 子类有可能访问了父类对象, 比如在构造函数 ...