Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29799   Accepted: 12090

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

tarjan模板
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
#define N 10010
vector<int>grap[N];//稀疏图,用邻接表表示图
stack<int>s;//栈
int low[N];//low[u] 为u或u的子树能够追溯到的最早的栈中节点的次序编号
int dfn[N];//dfn[u] 为u搜索的次序编号(时间戳)
int mark[N];//标记是否在栈中
int id[N];//id[i] = j 表示原图的点i缩点后为点j
int pd;//顶点的前序编号
int sd;//记录总共将图缩成多少个点
int sum[N];//记录sd编号的有几个点缩成
void tarjan(int v){
low[v]=dfn[v]=++pd;
s.push(v);
mark[v]=;
for(int i=;i<grap[v].size();i++){
int w=grap[v][i];
if(!dfn[w]){
tarjan(w);
low[v]=min(low[v],low[w]);//v或v的子树能够追溯到的最早的栈中节点的次序编号
}
else if(mark[w]){//(v,w)为后向边
low[v]=min(low[v],dfn[w]);
}
}
int u;
if(low[v]==dfn[v]){//满足强连通分支条件,进行缩点
sd++;
do{
u=s.top();
s.pop();
id[u]=sd;//缩点
sum[sd]++;
mark[u]=;//出栈解除标记 }while(u!=v);
}
}
int main(){
int n,m,a,b;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
grap[a].push_back(b);
}
for(int i=;i<=n;i++){
if(!dfn[i]) tarjan(i);
}
//if(sd==1) {printf("0\n");return 0;}//如果图已经为强连通图,over
int in[N]={},out[N]={};
for(int i=;i<=n;i++){//求缩点后,各个顶点的出度和入度
for(int j=;j<grap[i].size();j++){
int k=grap[i][j];
if(id[i]!=id[k]){
in[id[k]]++;
out[id[i]]++;
}
}
}
int ans=,p=;
for(int i=;i<=sd;i++){
if(!out[i]){
ans++;p=i;
}
}
printf("%d\n",ans==?sum[p]:);
return ;
}

poj2816的更多相关文章

随机推荐

  1. 3、列表 list

    列表 >>> list=['aaa','bbb','ccc'] >>> print list ['aaa', 'bbb', 'ccc'] >>> ...

  2. 开源 java CMS - FreeCMS2.3会员我的留言

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/29631.html​ 项目地址:http://www.freeteam.cn/ 我的留言 从 ...

  3. angular 中的$event 对象包含了浏览器原生的event对象

    ou can pass the $event object as an argument when calling the function. The $event object contains t ...

  4. cmd.exe启动参数说明

    启动命令解释程序 Cmd.exe 的新范例.如果在不含参数的情况下使用,cmd 将显示操作系统的版本和版权信息. 语法 cmd [{/c | /k}] [/s] [/q] [/d] [{/a | /u ...

  5. JAVA泛化及为什么需要泛化

    泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数. 比如,有一种类型为List,此时该List可以是任意类型的列表,如Integer,String等等. 如果把List类型改为List ...

  6. Node.js 把图片流送到客户端

    效果: 代码: var http=require('http'); var fs=require('fs'); var path=require('path'); var mime=require(' ...

  7. 倍福TwinCAT(贝福Beckhoff)基础教程2.2 TwinCAT常见类型使用和转换_结构体

    在DUTs文件夹上右击添加结构体,结构体中可以放基本变量类型,也可以嵌套其他结构体   使用的时候,需要声明结构体的实例,然后按照类.属性的格式来读写变量,会有代码的自动提示   你也可以声明数组,类 ...

  8. C# 反编译工具

    justdecompile http://down.51cto.com/data/2067031 ILSpy http://www.fishlee.net/soft/ilspy_chs/

  9. scrollTop clientTop offsetTop scrollHeight clientHeight clientWidth的差别及使用方法

    这几个属性做滚动时会经经常使用到.现总例如以下: 首先定义一个div.样式例如以下: <style> *{ margin:0px; padding:0px;} body{ margin:0 ...

  10. SQL中拆分字符串substr及统计字符出现频数replace用法实例讲解

    一.拆分字符串为若干行 例一:要求将表emp中的'king'按照每行一个单词拆成四行 注意:substr(str,pos):截取pos位置开始的字符: substr(str,pos,len):从pos ...