题目大意:给出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;
}

HDU - 3836 Equivalent Sets (强连通分量+DAG)的更多相关文章

  1. hdu - 3836 Equivalent Sets(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...

  2. [tarjan] hdu 3836 Equivalent Sets

    主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...

  3. hdu 3836 Equivalent Sets

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...

  4. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  5. hdu 3836 Equivalent Sets(tarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

  6. hdu——3836 Equivalent Sets

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  7. hdu 3836 Equivalent Sets trajan缩点

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  8. hdu 3836 tarjain 求强连通分量个数

    // 给你一个有向图,问你最少加几条边能使得该图强连通 #include <iostream> #include <cstdio> #include <cstring&g ...

  9. hdu 4685 二分匹配+强连通分量

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...

随机推荐

  1. Codeforces Round #247 (Div. 2) ABC

    Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431  代码均已投放:https://github.com/illuz/Wa ...

  2. 深入Delphi下的DLL编程

    深入Delphi下的DLL编程 作者:岑心 引 言 相信有些计算机知识的朋友都应该听说过“DLL”.尤其是那些使用过windows操作系统的人,都应该有过多次重装系统的“悲惨”经历——无论再怎样小心, ...

  3. BeanPostProcessor使用心得

    最近想对项目中的所有bean进行一个代理.然后监控bean得方法的使用情况.         刚开始想的方法是:重写项目的beanFactory,然后再getBean的使用,对结果object进行一个 ...

  4. directio mysql 编绎选项

    http://www.myexception.cn/linux-unix/495407.html http://www.iyunv.com/thread-25950-1-1.html

  5. 狗日的rem

    rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...

  6. thymleaf th:text 和 th:utext 之间的区别

    1 th:text属性 可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本 文本连接:用“+”符号,若是变量表达式也可以用“|”符号 e.g. 若home.welc ...

  7. Linux内核的idle进程分析

    1. idle是什么 简单的说idle是一个进程,其pid号为 0.其前身是系统创建的第一个进程.也是唯一一个没有通过fork()产生的进程. 在smp系统中,每一个处理器单元有独立的一个执行队列,而 ...

  8. 数据库 DB MySQL 基本操作 CRUD 多表 MD

    操作数据库 创建数据库:create 创建一个名称为mydb1的数据库 create database mydb1; 创建一个使用gbk字符集的mydb2数据库 create database myd ...

  9. jsp button提交表单

    表单提交可以用submit 也可以用button,下面介绍下面三种方式: 方法1 <form action = "提交的地址"> <input type=&quo ...

  10. ReportStudio中创建日期提示默认值模板

    很多人已经知道可以通过JS给RS中的日期提示控件设置运行前的默认值---------例如: 日期时间段默认为上一个月的开始日和结束日 在系统所有的报表中都这样操作,我们如何快速的引入?和方便下次修改统 ...