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. git只拉取github部分代码的方法

    需求:github某个项目所有代码太大,有600+M,甚至更大:只需要拉取部分代码,一是可以降低网络消耗,二是可以降低磁盘占用 分析了下空间占用情况:发现每个项目下的.git/objects/pack ...

  2. Java源码阅读Vector

    1类签名与注释 public class Vector<E> extends AbstractList<E> implements List<E>, RandomA ...

  3. Python 并行任务技巧

    FROM:    http://segmentfault.com/a/1190000000382873 Python的并发处理能力臭名昭著.先撇开线程以及GIL方面的问题不说,我觉得多线程问题的根源不 ...

  4. zabbix通过percona插件监控mysql

    percona zabbix mysql-plugin是percona发布的一个使用zabbix监控mysql数据库的工具,这款工具比zabbix自带的监控模板要强大的多,毕竟percona是Mysq ...

  5. C#控件之DataGridView

    第一种:DataSet ds=new DataSet (); this.dataGridView1.DataSource=ds.Table[0]; 第二种:DataTable dt=new DataT ...

  6. Convert WebP to PNG using java

    WebP是谷歌的图片格式,java 类库imageio 是不支持此种格式的.眼下除了在线转换以及工具以外,第三方类库转换webp格式大致有: 1.linux:Google libwebp 既是类库也能 ...

  7. EffectiveJava(8)覆盖equals是要遵守的约定

    覆盖equals是要遵守的约定 1.覆盖种类: -类的每个1实例本质上都是唯一的 -不关心类是否提供了"逻辑相等"的测试功能(Random测试是否能随机相同数字) -超类已经覆盖了 ...

  8. 谷歌安卓UI自动化测试策略

    中文翻译版: 为了使大家确信"应做单元测试,就一定能做单元测试",谷歌测试工程师Mona El Mahdy专门写了一篇博客,提出了几种执行安卓应用用户界面单元测试的方法.Mahdy ...

  9. mysql left join查询没走索引

    SELECT t0.ID as id, t0.`NAME` as name, t0.PHONE as phone, t0.`CITY_CODE` as cityCode, t0.SHOOTING_TI ...

  10. 倍福TwinCAT(贝福Beckhoff)基础教程 松下官方软件开启报错伺服未就绪怎么办

    一般是伺服到电机的动力线没接好(请查看动力线接线是否正确)   更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123   我的在线 ...