题目大意:给出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. Transistor latch improves on/off circuitry

    Figure 1 shows an example of on/off circuitry commonly used in battery-operated devices. The p-chann ...

  2. ubuntn 内核升级到LINUX v4.11.8:

    升级到LINUX v4.11.8: http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.8/linux-headers-4.11.8-041108_ ...

  3. WebService如何抛出干净的异常

    转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...

  4. [SQL基础]入门

    目录 什么是SQL? SQL能做什么? RDBMS SQL常见数据类型 SQL语法 什么是SQL? 结构化查询语言(Structured Query Language)简称SQL. 结构化查询语言是一 ...

  5. EntityFramework:迁移工具入门

    背景 刚毕业做项目的时候,没有用“迁移”这个概念,系统发布和更新的过程让人非常痛苦,在学习 Ruby On Rails 的过程解除了“迁移”,以后的所有项目都会先确定好“迁移”的方案,本文介绍一下En ...

  6. 浏览器User-Agent的详细信息

    PC端: safari 5.1 – MACUser-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit ...

  7. 彻底删除SVN版本库中部分文件或文件夹

    例:假设SVN库路径为E:/svn/project,库中的目录结构为 QA/Trunk Software/Tags/test.exe 删除Software/Tags/目录下的test.exe文件 操作 ...

  8. python的with和__enter__ 、 __exit__

    __enter__(): 在使用with语句时调用,会话管理器在代码块开始前调用,返回值与as后的参数绑定 __exit__():   会话管理器在代码块执行完成好后调用,在with语句完成时,对象销 ...

  9. Python任务调度模块 – APScheduler。动态修改调度时间间隔

    APScheduler可以把调度任务放到内存里,也可以把任务放到数据库里,那么如何交互式修改定时任务的执行时间间隔或者下次执行时间呢? 方案一:把定时任务放到数据库里,修改数据库里任务的调度时间 方案 ...

  10. VisualStudio使用GIT

    使用GIT进行源码管理 -- 在VisualStudio中使用GIT GIT作为源码管理的方式现在是越来越流行了,在VisualStudio 2012中,就通过插件的现实对GIT进行了官方支持,并且这 ...