POJ 2762 Going from u to v or from v to u? (有向图求单连通性)
来源:互联网 发布:淘宝买台式电脑靠谱吗 编辑:程序博客网 时间:2024/05/22 17:23
POJ 2762 Going from u to v or from v to u?
链接:http://poj.org/problem?id=2762
题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中。洞穴中有n 个房间,有一些单向的通道连接某些房间。每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x。Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成。为了使Wind 下达任务更容易些,Jiajia 决定找这样的一个洞穴,每对房间(设为x 和y)都是相通(可以从x 走到y,或者可以从y 走到x)的。给定一个洞穴,你能告诉Jiajia,Wind 是否可以任意选择两个房间而不用担心这两个房间可能不相通吗?
思路:这道题的本质是求有向图的单连通性。
单连通性:对于有向图中的任意两点u,v。一定存在从u到v的路径或是从v到u的路径。
1. 先对图求一次强连通分量,然后将每个强连通分量内的点缩成一个点。缩点之后便是一个DAG(有向无环图)。
2. 在DAG中,如果这个树有分叉,对于分叉的两点一定是不能互相到达的。所以该图一定要是一个单链才行。
3. 用拓扑排序来判断是否为单链,每次将入度为0的点加入队列,如果同时两个以上的点入度为0,那么这两个点一定是不能互相到达的,即为非单连通。
代码:
/*ID: wuqi9395@126.comPROG:LANG: C++*/#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<fstream>#include<cstring>#include<ctype.h>#include<iostream>#include<algorithm>using namespace std;#define INF (1<<30)#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))#define rep(i, a, n) for (int i = a; i < n; i++)#define per(i, a, n) for (int i = n - 1; i >= a; i--)#define eps 1e-6#define debug puts("===============")#define pb push_back#define mkp make_pair#define all(x) (x).begin(),(x).end()#define fi first#define se second#define SZ(x) ((int)(x).size())#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)typedef long long ll;typedef unsigned long long ULL;const int maxn = 1100;vector<int> g[maxn];int dfn[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;stack<int> s;void dfs(int u) { dfn[u] = low[u] = ++ dfs_clock; s.push(u); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!dfn[v]) { dfs(v); low[u] = min(low[u], low[v]); //通过儿子更新low值 } else if (!sccno[v]) low[u] = min(low[u], dfn[v]); //通过回边更新low值(且回边访问的点必须不在已划分的强连通分量中) } if (low[u] == dfn[u]) { //得到强连通分量 scc_cnt++; while(1) { int x = s.top(); s.pop(); sccno[x] = scc_cnt; if(x == u) break; } }}void find_scc(int n) { dfs_clock = scc_cnt = 0; memset(sccno, 0, sizeof(sccno)); memset(dfn, 0, sizeof(dfn)); while(!s.empty()) s.pop(); for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i);}const int maxm = 6100;int mp[maxm][2];vector<int> newg[maxn];int degree[maxn];void build_new_map(int m) { for (int i = 1; i <= scc_cnt; i++) newg[i].clear(), degree[i] = 0; for (int i = 0; i < m; i++) { int u = sccno[mp[i][0]], v = sccno[mp[i][1]]; if (u != v) { degree[v]++; newg[u].pb(v); } }}bool toposort() { queue<int> q; for (int i = 1; i <= scc_cnt; i++) if (!degree[i]) q.push(i); if (q.size() > 1) return 0; int tot = 0; while(!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < newg[u].size(); i++) { int v = newg[u][i]; degree[v]--; if (!degree[v]) q.push(v); } if (q.size() > 1) return 0; } return 1;}int main () { int n, m; int t; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) g[i].clear(); int u, v; rep(i, 0, m) { scanf("%d%d", mp[i], mp[i] + 1); g[mp[i][0]].pb(mp[i][1]); } find_scc(n); build_new_map(m); if (toposort()) puts("Yes"); else puts("No"); } return 0;}
0 0
- POJ 2762 Going from u to v or from v to u? (有向图求单连通性)
- POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or kosaraju
- POJ 2762 Going from u to v or from v to u? (图论-tarjan)
- POJ 2762 Going from u to v or from v to u?(强连通+拓扑)
- poj 2762 Going from u to v or from v to u?(强连通分量)
- poj 2762 Going from u to v or from v to u?(Tarjan+拓扑排序)
- POJ 2762 Going from u to v or from v to u(弱连通分量)
- POJ2762-Going from u to v or from v to u?(有向图强连通分量缩点)
- POJ 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u
- poj 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u?(Tarjan)
- poj 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u? 强连通最长链
- 二叉树(3):对二叉树数的操作
- LeetCode 67 Maximal Rectangle
- Conversion to Dalvik format failed with error 1
- AES加密
- cocos2d中CREATE_FUNC
- POJ 2762 Going from u to v or from v to u? (有向图求单连通性)
- Leetcode【42】:Multiply Strings
- 将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)
- 黑马程序员23——OC之内存管理(set方法的内存管理)
- C#操作XML小结
- 避免误用 Redis
- 数牛面向对象解析
- ios开发常见考题-最全版本
- 程序员生存定律-六个程序员的故事(2)