POJ 2186 tarjan+缩点 基础题
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 37111 | Accepted: 15124 |
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
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
const int maxn = 1e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
int mp[maxn];
struct node
{
int v,next;
}edge[maxn];
int dfn[maxn],low[maxn],index,visit[maxn],cnt,tot;
int point[maxn];
int heads[maxn],stack[maxn],num;
void add(int x,int y)
{
edge[++cnt].next=heads[x];
edge[cnt].v=y;
heads[x]=cnt;
return;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
stack[++index]=x;
visit[x]=;
for(int i=heads[x];i!=-;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
tarjan(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(visit[edge[i].v])
{
low[x]=min(low[x],dfn[edge[i].v]);
}
}
if(low[x]==dfn[x])
{
do{
int temp=stack[index];
point[temp]=num;
visit[temp]=;
index--;
}while(x!=stack[index+]);
num++;
}
return;
}
int main()
{
int n,m;
memset(heads,-,sizeof(heads));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
tot=cnt=num=index=;
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
int sum=;
for(int i=;i<=n;i++)
{
for(int j=heads[i];j!=-;j=edge[j].next)
{
if(point[i]!=point[edge[j].v])
{
mp[point[i]]=;
}
}
}
int cont=,index;
for(int i=;i<num;i++)
if(mp[i]==)
{
cont++,index=i;
}
if(cont==)
{
int ans=;
for(int i=;i<=n;i++)
if(point[i]==index)
ans++;
cout<<ans<<endl;
}
else
cout<<""<<endl;
return ;
}
推荐两篇博客 https://blog.csdn.net/qq_34374664/article/details/77488976
http://blog.miskcoo.com/2016/07/tarjan-algorithm-strongly-connected-components
POJ 2186 tarjan+缩点 基础题的更多相关文章
- poj 2186(tarjan+缩点)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37083 Accepted: 15104 De ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
- POJ - 2186 Popular Cows tarjain模板题
http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...
- POJ 3694 (tarjan缩点+LCA+并查集)
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...
- poj 2186 tarjan求强连通分量
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...
- poj 2186 (强连通缩点)
题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...
- POJ 2672 Tarjan + 缩点 + 拓扑思想
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17383 ...
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
随机推荐
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...
- WPF学习09:数据绑定之 Binding to List Data
从WPF学习03:Element Binding我们可以实现控件属性与控件属性的绑定. 从WPF学习07:MVVM 预备知识之数据绑定 我们可以实现控件属性与自定义对象属性的绑定. 而以上两个功能在实 ...
- R in action读书笔记(4)-第六章:基本图形(下)
6.3直方图 hist() 其中的x是一个由数据值组成的数值向量.参数freq=FALSE表示根据概率密度而不是频数绘制图形.参数breaks用于控制组的数量.在定义直方图中的单元时,默认将生成等距切 ...
- Request.Form("id")与Request.QueryString("id")的区别
获取以表单提交的post类型的值,也就是获取<form method="post">这种方式提交的值request包含各种方式的值,没有指名是什麽,所以Request. ...
- python 内置2to3工具将python2代码转换为python3代码
python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...
- OpenMP入门教程(三)
承接前面两篇,这里直接逐一介绍和使用有关OpenMP的指令和函数 Directives 1.for 作用:for指令指定紧随其后的程序的循环的迭代必须由团队并行执行,只是假设已经建立了并行区域,否则它 ...
- 他山之石:D3DX书籍推荐
一.DirectX9.0.3D游戏开发编程基础(龙书) 这本书网上评论很好,当初作为入门书看确实还不错,但是看得有点稀里糊涂的.现在回过头来看,它有些地方省略了,所以还得自己多多总结,总得来说还是值得 ...
- 借助百度地图API制作企业百度地图
做网站需要插入地图,可以借助百度地图API,具体步骤如下: 1.打开百度地图API的网址: http://api.map.baidu.com/lbsapi/creatmap/ 2.设置中心点 3. ...
- oracle分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总
语法:sum(col1) over(partition by col2 order by col3 ) 准备数据: DEPT_ID ENAME SAL1 1000 ...
- 黑马毕向东Java基础知识总结
Java基础知识总结(超级经典) 转自:百度文库 黑马毕向东JAVA基础总结笔记 侵删! 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部 ...