zoukankan      html  css  js  c++  java
  • HDU

    题目大意:给出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 "); 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 ", max(a, b)); } int main() { while (scanf("%d%d", &n, &m) != EOF) { init(); solve(); } return 0; }

  • 相关阅读:
    vim常用命令
    re正则表达式
    time和datetime
    airtestpython连接airtest及生产报告
    Java中的监视器(monitor)是什么?
    CentOS6.9安装MySQL5.7
    CentOS6不能使用yum问题修复:Cannot find a valid baseurl for repo: base
    谁说BI是新兴的有前途的技术,说穿了,就是原来的报表系统
    Trained myself of Informatica 6.0 (part 2 Installation).
    开始看lcc编译器的实现。---跟.net暂时扯不上关系。
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7290731.html
Copyright © 2011-2022 走看看