hdu 1269 迷宫城堡 强连通分量
2016-10-26 11:14
471 查看
Problem:
给一个有向图,问这个图是否两两都可互相达。
Solution:
求强连通分量,看一共有几个强连通分量,如果只有一个则两两可互相达。
给一个有向图,问这个图是否两两都可互相达。
Solution:
求强连通分量,看一共有几个强连通分量,如果只有一个则两两可互相达。
#include<cstdio> #include<iostream> #include<sstream> #include<cstdlib> #include<cmath> #include<cctype> #include<string> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<set> #include<map> #include<ctime> #include<vector> #include<fstream> #include<list> using namespace std; #define ms(s) memset(s,0,sizeof(s)) typedef unsigned long long ULL; typedef long long LL; const double PI = 3.141592653589; const int INF = 0x3fffffff; int n, m; const int maxn = 10010; stack<int> stk; vector<int> G[maxn]; int dfn[maxn], low[maxn], vis[maxn]; int times, tot;//强连通分量的个数 void tarjan(int u){ dfn[u] = low[u] = ++times; stk.push(u); vis[u] = -1; for(int i = 0; i < G[u].size(); ++i){ int v = G[u][i]; if(vis[v] == 0){ tarjan(v); low[u] = min(low[u], low[v]); } else{ if(vis[v] == -1) low[u] = min(low[u], dfn[v]); } } if(dfn[u] == low[u]){//u是一个强连通分量的根 tot++; while(!stk.empty()){ int v = stk.top(); stk.pop(); vis[v] = 1; if(v == u) break; } } } void solve(int n){ for(int i = 1; i <= n; ++i){ if(vis[i] == 0) tarjan(i); } } int main(){ // freopen("E:\\input.txt","r",stdin); // freopen("/home/really/Document/output","w",stdout); // ios::sync_with_stdio(false); int a,b; while(scanf("%d%d",&n,&m) && (n||m)){ times = tot = 0; ms(vis); while(!stk.empty()) stk.pop(); for(int i = 1; i <= n; ++i) G[i].clear(); for(int i = 0; i < m; ++i){ scanf("%d%d",&a,&b); G[a].push_back(b); } solve(n); if(tot <= 1) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- [HDU 1269] 迷宫城堡 强连通分量
- hdu 1269 迷宫城堡(强连通分量 Tarjan算法)
- HDU - 1269 迷宫城堡(强连通分量)
- hdu 1269 迷宫城堡 强连通分量
- hdu 1269 迷宫城堡 tarjan算法求有向图的强连通分量
- HDU 1269-迷宫城堡(强连通分量)
- hdu 1269 迷宫城堡(Tarjan 图的强连通分量)
- hdu1269 迷宫城堡 (简单强连通分量)
- HDU 1269 迷宫城堡(强连通分量)
- HDU_1269_迷宫城堡(强连通分量)
- 【HDU】 1269 迷宫城堡 强连通分量
- HDU-1269 迷宫城堡 (强连通分量[Kosaraju])
- HDU 1269 迷宫城堡(求是否只有一个强连通分量)
- HDU 1269 迷宫城堡(强连通分量)
- HDU 1269 迷宫城堡 (强连通分量,常规)
- HDU - 1269 迷宫城堡(有向图的强连通分量)
- hdu 1269 迷宫城堡(求强连通分量)
- hdu 1269 迷宫城堡(强连通分量)
- HDU 1269 迷宫城堡 (有向图的强连通分量)
- HDU 1269:迷宫城堡 (Tarjan强连通分量)