Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31533   Accepted: 12817

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. 
 
【题目分析】
  题目大概是有好多牛,牛(们)之间具有某种关系(鬼知道),这种关系具有传递性,如果有 A欢迎B和B欢迎C,那么就有A欢迎C。我们要找到能让其他所有的牛都欢迎的牛。
  (废话真多)
  就是求强连通分量了,然后找到一个强连通分量,缩点,最后找出度为0的点(这里可以自己草纸划一下),如果有多个出度为0的点,那么肯定没有牛被所有牛欢迎,如果只有一个,输出这个点所代表的强连通分量的长度!!!md
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=;
vector<int>tu[maxn];
vector<int>lt[maxn];
int n,m,lts=;
int js=;
int dfn[maxn],low[maxn];
int zhan[maxn],top=;
bool isins[maxn];
int num[maxn];//num[i]表示i点所在的强连通分量的编号
int d[maxn];
void tarjan(int i)//hhhh
{
int j;
dfn[i]=low[i]=++js;
isins[i]=;
zhan[top++]=i;
for(int j=;j<tu[i].size();j++)
{
int tp=tu[i][j];
if(dfn[tp]==-)
tarjan(tp),
low[i]=min(low[i],low[tp]);
else if(isins[tp])
low[i]=min(low[i],dfn[tp]);
}
if(dfn[i]==low[i])
{
lts++;
do{
j=zhan[--top];
isins[j]=;
lt[lts].push_back(j);
num[j]=lts;//j点所在的强连通分量的编号为lts
}while(i!=j);
}
}
void solve(int n)
{
memset(dfn,-,sizeof dfn);
memset(low,-,sizeof low);
memset(zhan,-,sizeof zhan);
memset(isins,,sizeof isins);
for(int i=;i<n;i++)
if(dfn[i]==-)
tarjan(i);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
tu[x-].push_back(y-);//我从0开始啊
}
solve(n);
for(int i=;i<n;i++)
for(int j=;j<tu[i].size();j++)
{
int t=tu[i][j];
if(num[i]!=num[t])//如果i点与他指向的点不在同一个强连通分量中
d[num[i]]++;//i点所在的强连通分量的出度+1
}
int pos=-;
int cnt=;
for(int i=;i<=lts;i++)
if(d[i]==)//找到出度为0的强连通分量
cnt++,
pos=i;
if(cnt==) cout<<lt[pos].size();//如果只有一个出度为0的强连通分量,那么这个强连通分量的长度即答案
else cout<<"";//woc
return ;
}

Poj2186Popular Cows的更多相关文章

  1. poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)

    /* 题目大意:有N个cows, M个关系 a->b 表示 a认为b popular:如果还有b->c, 那么就会有a->c 问最终有多少个cows被其他所有cows认为是popul ...

  2. 图论:POJ2186-Popular Cows (求强连通分量)

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  3. poj--2186--Popular Cows (scc+缩点)

    Popular Cows Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  4. poj2186Popular Cows(强连通分量)

    http://poj.org/problem?id=2186 用tarjan算出强连通分量的个数 将其缩点 连成一棵树  则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根 #includ ...

  5. poj2186-Popular Cows【Tarjan】+(染色+缩点)(经典)

    <题目链接> 题目大意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但 ...

  6. poj2186-Popular Cows(强连通分支)

    有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1 ...

  7. POJ-2186-Popular Cows(强连通分量,缩点)

    链接:https://vjudge.net/problem/POJ-2186 题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个 ...

  8. poj2186Popular Cows+tarjan缩点+建图

    传送门: 题意: 给出m条关系,表示n个牛中的崇拜关系,这些关系满足传递性.问被所有牛崇拜的牛有几头: 思路: 先利用tarjan缩点,同一个点中的牛肯定就是等价的了,建立新的图,找出其中出度为0的点 ...

  9. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

随机推荐

  1. LeetCode Find All Duplicates in an Array

    原题链接在这里:https://leetcode.com/problems/find-all-duplicates-in-an-array/ 题目: Given an array of integer ...

  2. oracle not in,not exists,minus 数据量大的时候的性能问题

    http://blog.csdn.net/greenappple/article/details/7073349/ 耗时 minus<not exists<not in

  3. sys.stdout.write与sys.sterr.write(三)

    目标: 1.使用sys.stdout.write模拟"|"的顺时针变化- \ | / 2.使用sys.stderr.write模拟"|"的顺时针变化- \ | ...

  4. Hibernate操作指南-实体与常用类型的映射以及基本的增删改查(基于注解)

  5. svn 架设

    1.yum install subversion  openssl-devel -y 2. cd /data/svn 3. svnadmin create remote 4. 编辑conf 下 aut ...

  6. spring Date JPA的主要编程接口

    Repository:最顶层的接口,是一个空接口,主要目的是为了同一所有Repository的类型,并且让组件扫描的时候自动识别. CrudRepository:是Repository的子接口,提供增 ...

  7. Excel 同时打开2个或多个独立窗口

    首先win7版本点击[开始]菜单,在输入框里面输入"regedit.exe"打开注册表     然后定位找到该路径HKEY_CLASSES_ROOT \ Excel.Sheet.1 ...

  8. FTPClient使用中的问题--获取当前工作目录为null

    使用org.apache.commons.net.ftp.FTPClient 来做ftp的上传下载功能 FTPClient ftp = new FTPClient();ftp.connect(doc. ...

  9. js判断变量是否等于undefined

    js中判断变量是否等于undefined,不是使用==,而是使用typeof. typeof(featureId)!="undefined"

  10. CSS3 笔记二(Gradients)

    CSS3 Gradients Two types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial ...