HDU 1269 迷宫城堡(向量)(Tarjan模版题)
迷宫城堡
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19017 Accepted Submission(s):
8328
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include<stack>
using namespace std; #define MAXN 10010
#define MAXM 100010
stack<int>s;
int head[MAXN],dfn[MAXN], low[MAXN], belong[MAXM];
int instack[]; // instack[]为是否在栈中的标记数组
int n, m, cnt, scnt, top, tot;
struct Edge
{
int v, next;
}e[MAXM]; //边结点数组 void add(int u,int v)
{
e[++cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt; }
void init()
{
cnt = ;
scnt=; //初始化连通分量标号
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn)); //结点搜索的次序编号数组为0,同时可以当是否访问的数组使用
while(!s.empty()) s.pop();
} void Tarjan(int v)
{
int min,t;
dfn[v]=low[v]=cnt++;
instack[v]=;
s.push(v);
for(int i=head[v];i!=-;i=e[i].next)
{
int j=e[i].v;
if(!dfn[j])//未被访问
{
Tarjan(j);
// 更新结点v所能到达的最小次数层
if(low[v]>low[j])
low[v]=low[j];
}
else if(instack[j]&&low[v]>dfn[j])
{//如果j结点在栈内,
low[v]=dfn[j];
}
}
if(dfn[v]==low[v])
{//如果节点v是强连通分量的根
scnt++;
do
{
t=s.top();
s.pop();
instack[t]=;
belong[t]=scnt;
}
while(t!=v);
}
} int main()
{
while(scanf("%d%d",&n,&m) && (n || m))
{
init();
while(m--)
{
int u,v;
scanf("%d%d", &u, &v);
add(u,v);
}
cnt=;
for(int i = ; i <= n; i++) //枚举每个结点,搜索连通分量
{
if(!dfn[i]) //未被访问
{
Tarjan(i); //则找i结点的连通分量 }
}
if(scnt == ) printf("Yes\n"); //只有一个强连通分量,说明此图各个结点都可达
else printf("No\n");
}
return ;
}
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0
No
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include<stack>
using namespace std; #define MAXN 10010
#define MAXM 100010
stack<int>s;
int head[MAXN],dfn[MAXN], low[MAXN], belong[MAXM];
int instack[]; // instack[]为是否在栈中的标记数组
int n, m, cnt, scnt, top, tot;
struct Edge
{
int v, next;
}e[MAXM]; //边结点数组 void add(int u,int v)
{
e[++cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt; }
void init()
{
cnt = ;
scnt=; //初始化连通分量标号,次序计数器,栈顶指针为0
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn)); //结点搜索的次序编号数组为0,同时可以当是否访问的数组使用
while(!s.empty()) s.pop();
} void Tarjan(int v)
{
int min,t;
dfn[v]=low[v]=cnt++;
instack[v]=;
s.push(v);
for(int i=head[v];i!=-;i=e[i].next)
{
int j=e[i].v;
if(!dfn[j])
{
Tarjan(j);
if(low[v]>low[j])
low[v]=low[j];
}
else if(instack[j]&&low[v]>dfn[j])
{
low[v]=dfn[j];
}
}
if(dfn[v]==low[v])
{
scnt++;
do
{
t=s.top();
s.pop();
instack[t]=;
belong[t]=scnt;
}
while(t!=v);
}
} int main()
{
while(scanf("%d%d",&n,&m) && (n || m))
{
init();
while(m--)
{
int u,v;
scanf("%d%d", &u, &v);
add(u,v);
}
cnt=;
for(int i = ; i <= n; i++) //枚举每个结点,搜索连通分量
{
if(!dfn[i]) //未被访问
{
Tarjan(i); //则找i结点的连通分量 }
}
if(scnt == ) printf("Yes\n"); //只有一个强连通分量,说明此图各个结点都可达
else printf("No\n");
}
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; #define MAXN 10010
#define MAXM 100010 struct Edge
{
int v, next;
}edge[MAXM]; //边结点数组 int first[MAXN], stack[MAXN], DFN[MAXN], Low[MAXN], Belong[MAXM];
// first[]头结点数组,stack[]为栈,DFN[]为深搜次序数组,Belong[]为每个结点所对应的强连通分量标号数组
// Low[u]为u结点或者u的子树结点所能追溯到的最早栈中结点的次序号
int instack[]; // instack[]为是否在栈中的标记数组
int n, m, cnt, scnt, top, tot; void init()
{
cnt = ;
scnt = top = tot = ; //初始化连通分量标号,次序计数器,栈顶指针为0
memset(first, -, sizeof(first));
memset(DFN, , sizeof(DFN)); //结点搜索的次序编号数组为0,同时可以当是否访问的数组使用
} void read_graph(int u, int v) //构建邻接表
{
edge[tot].v = v;
edge[tot].next = first[u];
first[u] = tot++;
} void Tarjan(int v) //Tarjan算法求有向图的强连通分量
{
int min, t;
DFN[v] = Low[v] = ++tot; //cnt为时间戳
instack[v] = ; //标记在栈中
stack[top++] = v; //入栈
for(int e = first[v]; e != -; e = edge[e].next)
{ //枚举v的每一条边
int j = edge[e].v; //v所邻接的边
if(!DFN[j])
{ //未被访问
Tarjan(j); //继续向下找
if(Low[v] > Low[j]) Low[v] = Low[j]; // 更新结点v所能到达的最小次数层
}
else if(instack[j] && DFN[j] < Low[v])
{ //如果j结点在栈内,
Low[v] = DFN[j];
}
}
if(DFN[v] == Low[v])
{ //如果节点v是强连通分量的根
scnt++; //连通分量标号加1
do
{
t = stack[--top]; //退栈
instack[t] = ; //标记不在栈中
Belong[t] = scnt; //出栈结点t属于cnt标号的强连通分量
}while(t != v); //直到将v从栈中退出
}
} void solve()
{
for(int i = ; i <= n; i++) //枚举每个结点,搜索连通分量
if(!DFN[i]) //未被访问
Tarjan(i); //则找i结点的连通分量
} int main()
{
while(scanf("%d%d",&n,&m) && (n || m))
{
init();
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
read_graph(u, v);
}
solve(); //求强连通分量
if(scnt == ) printf("Yes\n"); //只有一个强连通分量,说明此图各个结点都可达
else printf("No\n");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector> using namespace std;
vector<int>gh1[];//向量
vector<int>gh2[];
int cnt;
int vir[];
void dfs1(int n)
{
int x;
vir[n] = ;
cnt++;
for (int i = ; i<gh1[n].size(); i++)
{
x = gh1[n].at(i);
if (vir[x] == )
dfs1(x);
}
}
void dfs2(int n)
{
int x;
vir[n] = ;
cnt++;
for (int i = ; i<gh2[n].size(); i++)
{
x = gh2[n].at(i);
if (vir[x] == )
dfs2(x);
}
} int main()
{
int m, n, a, b;
while (~scanf("%d%d", &n, &m) && (m || n))
{
for (int i = ; i <= n; i++)
{
gh1[i].clear();
gh2[i].clear();
}
for (int i = ; i <= m; i++)
{
scanf("%d%d", &a, &b);
gh1[a].push_back(b);
gh2[b].push_back(a);
}
memset(vir, , sizeof(vir));
cnt = ;
dfs1();//正的走一次
if (cnt != n)
{
printf("No\n");
continue;
}
memset(vir, , sizeof(vir));
cnt = ;
dfs2();//反的走一次
if (cnt != n)
{
printf("No\n");
continue;
}
printf("Yes\n"); } return ;
}
HDU 1269 迷宫城堡(向量)(Tarjan模版题)的更多相关文章
- HDU 1269 迷宫城堡(强连通)
HDU 1269 迷宫城堡 pid=1269" target="_blank" style="">题目链接 题意:中文题 思路:强连通模板题 代 ...
- hdu 1269 迷宫城堡
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个 ...
- HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1269 迷宫城堡 最简单的联通图题 kosaraju缩点算法
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- HDU 1269.迷宫城堡-Tarjan or 双向DFS
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1269 迷宫城堡 (tarjan)
迷宫城堡Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 1269 迷宫城堡(Targin算法)
---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1269 -- 迷宫城堡【有向图求SCC的数目 && 模板】
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1269 迷宫城堡 (Kosaraju)
题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...
随机推荐
- 循环队列 c 实现!!!!
上数据结构课的时候老师让写了一个循环队列子系统. 代码如下: #include<stdio.h> #include<malloc.h> #define MAXLEN 100 # ...
- 打开ahci模式
打开ahci模式(sata)的正确步骤: 1.安装系统前在bios中将硬盘模式设置为ide兼容模式(别说你不会哦),然后安装系统,建议安装微软原版系统或是官方oem原版系统: 2.系统安装 ...
- DevExpress v17.2新版亮点——XAF篇
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了eXpressApp Framework v17.2 的新功能,快来下载试用新版本 ...
- spring数据源
包含三部分内容 1.spring jdbc 2. spring datasource 3.spring Connection pooling 完整的项目请往百度云盘下载: https://pan.ba ...
- SharePoint 2013的100个新功能之内容管理(四)
一:脚本编辑器Web部件 新的脚本编辑器Web部件表现为插入标签页下的Ribbon中的"嵌入的代码",可以使用户在SharePoint网站页面中添加HTML或Javascript或 ...
- <二叉树的基本操作>
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...
- Thumbnailator java图片压缩,加水印,批量生成缩略图
地址:http://code.google.com/p/thumbnailator/ 1.指定大小进行缩放 //size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 * 若图片横 ...
- SVN 将主干的代码合并到分支上
来源:http://blog.csdn.net/u012701023/article/details/50978154 问题:开发有了项目主干,再次基础上起了一个分支,开发新的功能:因为业务需要,在上 ...
- SVN命令行使用总结
1.上传项目到SVN服务器上svn import project_dir(本地项目全路径) http://192.168.1.242:8080/svn/IOS/Ben/remote_dir(svn项目 ...
- shell学习笔记汇总
1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...