poj2816
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 29799 | Accepted: 12090 |
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A
thinks B is popular.
Output
who are considered popular by every other cow.
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
#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的更多相关文章
随机推荐
- 模式识别hw2-------基于matconvnet,用CNN实现人脸图片性别识别
主要来源模式识别课程大作业,本文首先感谢当初的助教和一起完毕作业的队友 matconvnet在matlab下封装了CNN常见算法,网址http://www.vlfeat.org/matconvnet/ ...
- Hive图形化界面客户端
通过JDBC连接HiveServer2的图形界面工具,包括:SQuirrel SQL Client.Oracle SQL Developer以及DbVisualizer SQuirrel SQL Cl ...
- swftools使用
为了支持gif转swf以及pdf转swf.编译swftools过程中遇见几个问题,记录一下. 首先下载swftools:http://www.swftools.org/ 它依赖几个包,这里我使用的版本 ...
- spring 配置多数据源 (案例)
*.properties配置: <!--数据连接配置一--> jdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:m ...
- redis中的事务(版本2.6.16)
一.命令支持 1.multi 开始事务 2.exec事务提交 3.取消事务discard 二.事务示例 1.示例 redis>set key1 20OKredis>mutilOKredis ...
- Linux——环境变量的文件及配置
环境变量是包含关于系统及当前登录用户的环境信息的字符串,一些软件程序使用此信息确定在何处放置文件(如临时文件). 一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker ...
- react-navigation + react-native-vector-icons
1.安装 yarn add react-navigation react-native-vector-icons 2.创建 root.js import React, {Component} from ...
- odoo分析会计
odoo财务会计凭证录入时,支持 在凭证行 输入 分析账户和 分析标签 如果凭证行设置了 分析账户或者分析标签, 则在会计凭证过账的时候, 在分析会计功能建立 分析会计分录 其中, 如果设置了分析账户 ...
- jQuery.delegate() 函数详解
delegate()函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 即使是执行delegate()函数之后新添加的元素,只要它符合条件,绑定 ...
- java 字符深入知识,待整理
'编',"编", 为什么获取到的字节数组长度不一样 http://www.cnblogs.com/yongdaimi/p/5899328.html Unicode 官网 http ...