POJ 2762 Going from u to v or from v to u? 强连通分量+DAG最长路
来源:互联网 发布:做小报软件 编辑:程序博客网 时间:2024/05/16 10:40
题意:给你一个有向图,问你是否对于任意两个点都有一条u->v或者v->u的路。
思路:对于一个强联通分量,里面的点一定满足条件,我们可以把他们缩点,而对于不在同一联通分量的点,如果存在一条路径,使得这条路径能够遍历所有点,即符合题意。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i, a, b ) for( int i = a; i < b; i++ )#define CLR( a , x ) memset( a , x , sizeof a )const int maxn = 1000 + 10;const int maxe = 20000 + 10;struct Edge{ int v , next; Edge (int v = 0, int next = 0) : v(v), next(next) {}};struct SCC{ int Head[maxn], cntE; int dfn[maxn], low[maxn], dfs_clock; int scc[maxn], scc_cnt; int Stack[maxn], top; bool ins[maxn]; Edge edge[maxe]; void init(){ top = 0; cntE = 0; scc_cnt = 0; dfs_clock = 0; CLR(ins, 0); CLR(dfn, 0); CLR(Head, -1); } void add(int u, int v){ edge[cntE] = Edge( v, Head[u]); Head[u] = cntE++; } void Tarjan(int u){ dfn[u] = low[u] = ++dfs_clock; Stack[top++] = u; ins[u] = 1; for (int i = Head[u] ; ~i ; i = edge[i].next){ int v = edge[i].v; if (!dfn[v]){ Tarjan (v) ; low[u] = min(low[u], low[v]) ; } else if (ins[v]) low[u] = min (low[u], dfn[v]) ; } if (low[u] == dfn[u]){ ++scc_cnt; while ( 1 ){ int v = Stack[--top]; ins[v] = 0; scc[v] = scc_cnt; if (v == u) break; } } } void find_scc(int n){ REP(i, 0, n) if(!dfn[i]) Tarjan (i) ; }}scc;int n, m;int in[maxn];int dp[maxn];int H[maxn], cnte;Edge e[maxe];void Init(){ memset(H, -1, sizeof(H)); cnte = 0;}void Add(int u, int v){ e[cnte] = Edge(v, H[u]); H[u] = cnte++;}int DP(int u){ int &ans = dp[u]; if(ans > 0) return ans; for(int i = H[u]; ~i; i = e[i].next) ans = max(ans, DP(e[i].v) + 1); return ans;}void solve(){ scanf("%d%d", &n, &m); scc.init(); memset(dp, 0, sizeof(dp)); memset(in, 0, sizeof(in)); for(int i = 0; i < m; i++){ int u, v; scanf("%d%d", &u, &v); scc.add(u-1, v-1); } scc.find_scc(n); Init(); for(int i = 0; i < n; i++) for(int j = scc.Head[i]; ~j; j = scc.edge[j].next){ int u = i, v = scc.edge[j].v; if(scc.scc[u] != scc.scc[v]){ Add(scc.scc[u], scc.scc[v]); in[scc.scc[v]]++; } } int i; for(i = 1; i <= scc.scc_cnt; i++){ if(in[i] == 0){ DP(i); break; } } if(dp[i] == scc.scc_cnt - 1) printf("Yes\n"); else printf("No\n");}int main(){ int T; scanf("%d", &T); while(T--) solve(); return 0;}
0 0
- POJ 2762 Going from u to v or from v to u? 强连通分量+DAG最长路
- 【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?(强连通分量+拓扑排序)
- 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? 强连通+拓扑
- 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? (强连通分量[Tarjan]&&(拓扑排序||树形DP))
- POJ 2762 —— Going from u to v or from v to u? 强连通+拓扑
- Android Camera HAL3中预览preview模式下的数据流
- 【springmvc+mybatis项目实战】杰信商贸-34.业务出口报运WebService1
- html规范
- 360儿童卫士2智能手表
- ArcGIS JavaScript API相关资料收集
- POJ 2762 Going from u to v or from v to u? 强连通分量+DAG最长路
- 如何区分空间参考、坐标系统、投影、基准面和椭圆体?
- iPhone/iPad/iPod Touch各设备参数对比
- saber实用实例:基于理想开关的BUCK开环仿真
- Objective-C编码规范(三 ,四)
- 构造函数、复制构造函数和析构函数的总结
- 关于二分图的知识点零星总结
- Android studioAPK获取签名的方法
- centos7 安装 matplotlib的最便捷办法