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. js获取页面传来参数的方法

    function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  2. Android源码和内核源码的下载,编译和执行

    笔者依据罗升阳老师的<Android 系统源码情景分析>一书,尝试下载,编译和执行Android源码和内核源码.但可能是软件源"被墙"或版本号更新的原因.期间遇到诸多问 ...

  3. 【Hadoop】Hadoop DataNode节点超时时间设置

    hadoop datanode节点超时时间设置 datanode进程死亡或者网络故障造成datanode无法与namenode通信,namenode不会立即把该节点判定为死亡,要经过一段时间,这段时间 ...

  4. java gc log

    java full gc 经常带来延迟, 导致性能问题 如下命令使java虚拟机记录gc的log到文件, 帮助分析定位问题. java -Xloggc:./a.log -jar a.jar    // ...

  5. [转]解决Docker容器时间与主机不一致问题

    原文: https://blog.csdn.net/luckystar689/article/details/76572046 https://stackoverflow.com/questions/ ...

  6. 转: javascript技术栈

    http://www.infoq.com/cn/articles/state-of-javascript-2016

  7. JS杂技之无中间变量的值交换方式

    从http://www.cnblogs.com/liuyitian/p/4081517.html#3074553看到一种无中间变量的值交换方式,具体如下: var a = 1;var b = 2;a ...

  8. mkdir的参数-p的作用

    mkdir -p /nfs 也就是加上-p参数,之前只知道是递归创建目录,于是就发问了,得到的答案是: -p, --parents              no error if existing, ...

  9. TCP应用程序通信协议的处理

    TCP应用程序通信协议的处理 flyfish 2015-6-29 一 流式处理 TCP是一种流协议(stream protocol).TCP数据是以字节流的形式传递给接收者的,没有固有的"报 ...

  10. 创建标题栏,UINavigationBar的使用

    IOS 开发有关界面的东西不仅可以使用代码来编写,也可以使用Interface Builder可视化工具来编写.今天有个朋友问我这两个有什么区别,首先说说IB ,使用它编辑出来的控件其实底层还是调用代 ...