#include<stdio.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
#include<stack> #define N 1000
#define INF64 1152921504606846976
#define INF32 2147483647
#define ll int
using namespace std; vector<int>G[N],Tarjan[N];//Tarjan从0开始存下所有的强连通,其大小用 tar记录,注意分量中只有一个点时,则不连通(Tarjan[tar]<2 则应该为0 )
stack<int>mystack;
int n,m,tar;
int DFN[N],Low[N],Time;// 小写的time会redeclared
bool instack[N],vis[N]; inline ll Max(ll a,ll b){return a>b?a:b;}
inline ll Min(ll a,ll b){return a<b?a:b;} void tarjan(int u){
int v;
DFN[u]=Low[u]=++Time;
mystack.push(u); instack[u]=true;
vis[u]=true;
for(int i=0;i<G[u].size();i++)//遍历u的所有子节点
{
v=G[u][i];
if(!DFN[v])
{
tarjan(v);
Low[u]=Min(Low[u],Low[v]);
}
else if(instack[v])
Low[u]=Min(Low[u],DFN[v]);
}
if(DFN[u]==Low[u])
{
do
{
v=mystack.top(); mystack.pop(); instack[v]=false;
Tarjan[tar].push_back(v);
}while(u!=v);
tar++;
}
}
void InitTar(){
memset(DFN,0,sizeof(DFN)); memset(Low,0,sizeof(Low));
memset(instack,0,sizeof(instack));
memset(vis,0,sizeof(vis));//标记已搜过的点
while(!mystack.empty())mystack.pop();
for(int i=0;i<n;i++)Tarjan[i].clear();
tar=Time=0;
}
int main(){
int i,j;
while(~scanf("%d%d",&n,&m)){
for(i=0;i<n;i++)G[i].clear();
while(m--)
{
int u,v; scanf("%d %d",&u,&v);
G[u].push_back(v);
}
InitTar();
for(j=0;j<n;j++)//可能图是不连通的,所以枚举所有未去过的点
if(!vis[j]) tarjan(j);
}
return 0;
}

强连通tarjan模版的更多相关文章

  1. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  2. Tarjan模版(链式向前星表示方法)

    这道模版用到了链式向前星表示法: struct node { int v,next; }edge[]; void add(int x,int y) { edge[++cnt].next=heads[x ...

  3. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  4. HDOJ迷宫城堡(判断强连通 tarjan算法)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  5. HDU 1269 迷宫城堡(向量)(Tarjan模版题)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. CF:Problem 427C - Checkposts强连通 Tarjan算法

    tarjan算法第一题 喷我一脸. ...把手写栈的类型开成了BOOL.一直在找错.. . #include<cstdio> #include<cstring> #includ ...

  7. 【算法•日更•第二十八期】图论:强连通+Tarjan算法(一)

    ▎前言 一直都想学习这个东西,以为很难,结果发现也不过如此. 只要会些图论的基础就可以了. ▎强连通 ☞『定义』 既然叫强连通,那么一定具有很强的连通性. 强连通:就是指在一个有向图中,两个顶点可以互 ...

  8. hdu3594 强连通 tarjan

    题意: 判断是不是强连通图 ,同时每一条边必须只能在一个环里 思路:之前我的强连通用的全是双深搜,结果题目的第二个要求很难判断,一开始写了三个深搜加上并查集,结果越写越乱,其实就是在判断一个边是否只在 ...

  9. hdu1269迷宫城堡 (强连通Tarjan+邻接表)

    Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每一个通道都是单向的,就是说 ...

随机推荐

  1. eclipse 修改js文件无法编译到项目中

    1.场景重现 在今天修改js文件完善功能时,发现在eclipse中修改了文件后,刷新页面功能无法同步: 2.分析原因 查看编译路径,文件没有修改: 2.1 可能是缓存问题: 2.2 项目未编译: 3. ...

  2. 1020 Tree Traversals (25)(25 point(s))

    problem Suppose that all the keys in a binary tree are distinct positive integers. Given the postord ...

  3. ARM 内核

    ARM相关知识: ARM核:A8,ARM11,ARM9 指令架构:ARMv7,ARMv6,ARMv4 ARM核分为两个阵营: 经典型:ARM7,ARM9,ARM11 Cortex: Cortex A: ...

  4. Qt Quick快速入门之qml与C++交互

    C++中使用qml对象,直接使用findChild获取qml对象,然后调用setProperty方法设置属性,当然必须在加载qml之后才能使用,不然findChild找不到对象,用法如下. engin ...

  5. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  6. ZeptoLab Code Rush 2015 A. King of Thieves 暴力

    A. King of Thieves Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/pr ...

  7. JQ中get()与eq()的区别

    .eq() : 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() : 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区别呢? eq ...

  8. git命令行使用帮助

    克隆代码库 git clone git_project_url 提交代码 git commit -m 'commit messge'

  9. linux下TP5安装好Workerman 报错:Class 'think\worker\Server' not found

    今天把功能放到服务器,本地测试正常,上传到服务器上报错Class 'think\worker\Server' not found 首先想到的是Windows和Linux下大小写的问题,查看了代码,并没 ...

  10. SET XACT_ABORT ON 数据库事务

    转载:http://www.cnblogs.com/rob0121/articles/2320932.html SET XACT_ABORT ON SET XACT_ABORT ON分为两种: 1.总 ...