HDU - 3836 Equivalent Sets (强连通分量+DAG)
来源:互联网 发布:阿里云代理服务器 编辑:程序博客网 时间:2024/05/22 08:05
题目大意:给出N个点,M条边,要求你添加最少的边,使得这个图变成强连通分量
解题思路:先找出所有的强连通分量和桥,将强连通分量缩点,桥作为连线,就形成了DAG了
这题被坑了。用了G++交的,结果一直RE,用C++一发就过了。。。
#include <cstdio>#include <cstring>#define N 20010#define M 100010#define min(a,b) ((a) > (b)? (b): (a))#define max(a,b) ((a) > (b)? (a): (b))struct Edge{ int from, to, next;}E[M];int head[N], sccno[N], pre[N], lowlink[N], stack[N], in[N], out[N];int n, m, tot, dfs_clock, top, scc_cnt;void AddEdge(int from, int to) { E[tot].from = from; E[tot].to = to; E[tot].next = head[from]; head[from] = tot++;}void init() { memset(head, -1, sizeof(head)); tot = 0; int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); AddEdge(u, v); }}void dfs(int u) { pre[u] = lowlink[u] = ++dfs_clock; stack[++top] = u; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].to; if (!pre[v]) { dfs(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if (!sccno[v]) { lowlink[u] = min(lowlink[u], pre[v]); } } int x; if (pre[u] == lowlink[u]) { scc_cnt++; while (1) { x = stack[top--]; sccno[x] = scc_cnt; if (x == u) break; } }}void solve() { memset(sccno, 0, sizeof(sccno)); memset(pre, 0, sizeof(pre)); dfs_clock = top = scc_cnt = 0; for (int i = 1; i <= n; i++) if (!pre[i]) dfs(i); if (scc_cnt <= 1) { printf("0\n"); return ; } for (int i = 1; i <= scc_cnt; i++) in[i] = out[i] = 1; for (int i = 0; i < tot; i++) { int u = E[i].from, v = E[i].to; if (sccno[u] != sccno[v]) { out[sccno[u]] = in[sccno[v]] = 0; } } int a = 0, b = 0; for (int i = 1; i <= scc_cnt; i++) { if (out[i]) a++; if (in[i]) b++; } printf("%d\n", max(a, b));}int main() { while (scanf("%d%d", &n, &m) != EOF) { init(); solve(); } return 0;}
0 0
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
- HDU 3836 Equivalent Sets 强连通分量
- HDU 3836 Equivalent Sets(强连通分量)
- HDU 3836 Equivalent Sets 强连通分量
- HDU 3836 Equivalent Sets(强连通分量)
- hdu 3836 Equivalent Sets(强连通分量--加边)
- HDU 3836 - Equivalent Sets【强连通分量 基础题】
- HDU 3836Equivalent Sets 强连通分量分解
- HDU 3836 Equivalent Sets(强连通分量)
- HDU 3836:Equivalent Sets【强连通】
- 【强连通分量+缩点】 HDOJ 3836 Equivalent Sets
- HDOJ 题目3836 Equivalent Sets(强连通分量)
- HDU 3836 Equivalent Sets(强连通分量 Tarjan缩点)
- HDU 2767 Proving Equivalences && HDU 3836 Equivalent Sets 强连通
- HDU 3836 Equivalent Sets (tarjan求强联通分量)
- hdu 3836 Equivalent Sets(强连通,tarjan,4级)
- hdu 3836 Equivalent Sets【强连通Kosaraju+缩点染色】
- HDU 3836 Equivalent Sets(tarjan + 压缩强连通)
- 第30章:RatingBar的用法
- SYN Cookie的原理和实现
- 关于android性能,内存优化
- 每日20行之4~~~两种方法java实现简易计算器 附带复杂计算器代码(优先级顺序)
- 【写一个C程序验证系统是32bit/64bit,验证系统是大端对齐还是小端对齐】
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
- HDU 4279 Number(找规律)
- 【转载】设计之路:如何进行软件需求分析?
- 搭建java开发环境
- HDU - 1827 Summer Holiday(强连通分量+贪心)
- vsftpd 安装与使用
- php-实例1:简单的文章管理系统
- hadoop之家族
- ZOJ 3721 Final Exam Arrangement(贪心)