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 ...
随机推荐
- 不重启IIS修改dotnet framework版本
因为公司现在存在.net站点和asp站点共同运行的情况,所以需要对IIS进行一些修改,运行环境Win2003+IIS6 一.起因 原来的老站是asp开发的,用的是.net 2.0运行环境; 新站是.n ...
- WPF学习07:MVVM 预备知识之数据绑定
MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...
- AJPFX学习笔记JavaAPI之String类
学习笔记JavaAPI之String类 [size=10.5000pt]一.所属包java.lang.String,没有子类.特点:一旦被初始化就不可以被改变. 创建类对象的两种方式: String ...
- 白话容器namespace
进入正题之前是例行装X环节: 过年7天吃的,花了45天又回来了. 近年来容器大火,也正是因为容器,生生灭掉了一个IT岗位!哥也是被生生的逼上了邪路. 那究竟什么是容器呢? 就三个字:它就是个进程!(多 ...
- com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FileSize"
请求阿里云的OSS接口图片信息,返回json格式的数据,通过ObjectMapper将json转为Image对象时候报错转换失败 将json转对象的代码: String jsonStr = " ...
- Redux中的异步操作
异步操作的另一种方案就是让Action Creator返回一个Promise对象. 我们这边使用 redux-promise 中间件 import { createStore, applyMidd ...
- provider模式
最近看代码有所感想吧.当底层API,发生变化时,可以使用Provider模式.既然是模式就一定的股则. 1.该模式对原有接口的封装. 2.该模式实现对API的封装,不显示细节,从而取消依赖关系. 3. ...
- rem手机端页面自适应布局(待修正下一篇完美布局)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP常量和数据类型考察点
PHP 常量 常量是单个值的标识符(名称).在脚本中无法改变该值. 有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号). 注释:与变量不同,常量贯穿整个脚本是自动全局的. PHP常量的两种定 ...
- 说说windows10自带浏览器Edge的好与不好
用了10几个月了,正式版也升级了,今天来说说微软自带浏览器microsoft Edge的好与不好 先说好的吧 一,浏览器速度非常快,无论是打开还是关闭,或者是语音助手小娜需要调动浏 ...