HDU1269

判断强联通图

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cctype>
 6 #include <time.h>
 7 #include <string>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <vector>
12 #include <stack>
13 #include <algorithm>
14 #include <iostream>
15 using namespace std;
16 #define PI acos( -1.0 )
17 typedef long long ll;
18 typedef pair<int,int> P;
19 const double E = 1e-8;
20
21 const int NO = 10000 +5;
22 vector <int> nod[NO];
23 int mark[NO];
24 int n, m;
25
26 void dfs( int x )
27 {
28     mark[x] = 1;
29     for( int i = 0; i < nod[x].size(); ++i )
30         if( !mark[nod[x][i]] )
31             dfs( nod[x][i] );
32 }
33
34 int main()
35 {
36     while( ~scanf( "%d%d", &n, &m ) && n+m )
37     {
38         int x, y;
39         while( m-- )
40         {
41             scanf( "%d%d", &x, &y );
42             nod[x].push_back( y );
43         }
44         bool flag = true;
45         for( int i = 1; i <= n; ++i )
46         {
47             memset( mark, 0, sizeof( mark ) );
48             dfs( i );
49             for( int j = 1; j <= n; ++j )
50                 if( !mark[j] )
51                 {
52                     flag = false;
53                     break;
54                 }
55             if( !flag )
56                 break;
57         }
58         if( flag )
59             puts( "Yes" );
60         else
61             puts( "No" );
62         for( int i = 1; i <= n; ++i )
63             nod[i].clear();
64     }
65     return 0;
66 }
时间: 2024-08-07 17:04:42

HDU1269的相关文章

hdu1269 迷宫城堡,有向图的强连通分量 , Tarjan算法

hdu1269 迷宫城堡 验证给出的有向图是不是强连通图... Tarjan算法板子题 Tarjan算法的基础是DFS,对于每个节点.每条边都搜索一次,时间复杂度为O(V+E). 算法步骤: 1.搜索到某一个点时,将该点的Low值标上时间戳,然后将自己作为所在强连通分量的根节点(就是赋值Dfn=Low=time) 2.将该点压入栈. 3.当点p有与点p'相连时,如果此时p'不在栈中,p的low值为两点的low值中较小的一个. 4.当点p有与点p'相连时,如果此时p'在栈中,p的low值为p的lo

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

Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间.Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i. Input 输入包含多组数据,输

hdu1269 Tarjan强连通分量 模板(转)

#include<stdio.h> #include<iostream> #include<vector> using namespace std; const int maxn=10010; vector<int>g[maxn]; int Bcnt; int Top; int Index; int low[maxn],dfn[maxn]; int belong[maxn],stack[maxn]; int instack[maxn]; void Init_

hdu1269:强连通分量

判断是不是只有一个强连通分量就好了... ------------------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack>

hdu1269 强连通

题意:判断给定有向图中是否所有点都能够互相到达. 就是询问是否只有一个强连通分量. 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 7 const int maxn=1e4+5; 8 const int maxm=1e5+5; 9 10 int head[maxn],point[maxm],nxt[max

HDU1269——Tarjan——迷宫城堡

http://acm.hdu.edu.cn/showproblem.php?pid=1269 Tarjan的强连通算法: 对于每一个点有一个编号DFN和能往上一步的最小点的编号LOW 在进行深搜时,对于每一个节点都有一个编号 假定现在定点为u 深搜时有两种情况 1.v没有被搜过,继续搜,LOW[u] = min(LOW[u], LOW[v])(这是深搜v之后的结果) 2.v搜过了,LOW[u] = min(LOW[u], DFN[v])(注意这里的LOW定义的是上一个的编号,并不是一直能往上的最

HDU1269(有向图缩点模板题)

迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11070    Accepted Submission(s): 4948 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A

HDU1269 迷宫城堡 (强连通图判定)

题意:判定给出的有向图是不是强连通图 Tarjan算法模板题目 #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<set> #include<map> #include<string> #include<cstring> #include<stack> #include<queue&

HDU1269迷宫城堡

Tarjan算法解读:https://www.byvoid.com/zht/blog/scc-tarjan 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cstring> 5 #include<vector> 6 #include<stack> 7 #include<algorithm> 8 using namespace st