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的更多相关文章
随机推荐
- N++ 道ASP.NET面试题
InterviewQuestions-ASP.NET N++ 道ASP.NET面试题 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . ...
- Java List序列化的实现
概述 java中的序列化与反序列化都要求对象实现Serializable接口(其实就是声明一下),而对于List这种动态改变的集合默认是不实现这个接口的,也就是不能直接序列化.但是数组是可以序列化的, ...
- /profile文件修改后立即生效
修改profile etc/profile文件是只读的,直接用vi或gedit打开修改后是无法保存的.要修改profile,需要取得root权限,(使用gedit编辑) $sudo gedit /et ...
- EffectiveJava(13)使类和成员的可访问性最小化
1.为什么要使类和成员可访问性最小化 它可以有效地解除组成系统的各模块之间的耦合关系,使得这些模块可以独立的开发 测试 优化 使用 理解和修改.提高软件的可重用性 2.成员的访问级别 私有(priva ...
- Intellij idea远程debug连接tomcat,实现单步调试
转载:http://blog.csdn.net/boling_cavalry/article/details/73384036 web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是I ...
- Mismatched locale IDs. The component locale ID (2052) does not match the connection manager locale ID (2057)
Snapshot: When using the 'Flat File Source' and 'OLE DB Destination' or something else components to ...
- Java 二分法查找
算法:当数据量很大适宜采用该方法.采用二分法查找时,数据需是有序不重复的. 基本思想:假设数据是按升序排序的,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功:若 x 小于 ...
- 解决ie7下overflow:hidden失效问题
但父亲元素下面的子节点或者孙子节点有position:relative:或者absolute时,父亲即使设置了overflow:hidden:依然会溢出 解决方法可以: 在父亲元素上加上*positi ...
- Linux下的非阻塞IO(一)
非阻塞IO是相对于传统的阻塞IO而言的. 我们首先需要搞清楚,什么是阻塞IO.APUE指出,系统调用分为两类,低速系统调用和其他,其中低速系统调用是可能会使进程永远阻塞的一类系统调用.但是与磁盘IO有 ...
- 工作总结 用, 隔开数据 后台不可以用 List<string> 接收 get请求直接通过浏览器发请求传数组或者list到后台
旁边的 css js 为项目的 加载 css js 地址 只加载引用的样式 js http://localhost:8736/LinInFoKPI/ExcelPrint?line=&start ...