题目大意:给出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. Simple dc/dc converter increases available power in dual-voltage system

    The schematic in Figure 1 shows a way to increase the power available from a current-limited 5V supp ...

  2. Android Studio开发Android问题集【持续更新】

    问题一:emulator:ERROR:This AVD's configuration is missing a kernel file!! 答:打开Android SDK Manager,查看相应的 ...

  3. flush清空输入输出流

    #include <string.h> #include <stdio.h> #include <conio.h> #include <io.h> vo ...

  4. Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...

  5. 最新office2003密钥

    Microsoft Office Professional Edition 2003GWH28-DGCMP-P6RC4-6J4MT-3HFDY Office2003序列号注册码sn: WFDWY-XQ ...

  6. LoadLibrary文件路径及windows API相关的文件路径问题

    LoadLibrary HMODULE WINAPI LoadLibrary( _In_  LPCTSTR lpFileName ); Loads the specified module into ...

  7. go语言基础之defer延迟调用

    1.defer作用 关键字 defer ⽤于延迟一个函数或者方法(或者当前所创建的匿名函数)的执行.注意,defer语句只能出现在函数或方法的内部. 运行场景: defer语句经常被用于处理成对的操作 ...

  8. 对开源库使用 AutoCAD 文件格式[转]

    https://www.ibm.com/developerworks/cn/opensource/os-autocad/ 对开源库使用 AutoCAD 文件格式 读取 DWG 和 DXF 文件格式 C ...

  9. C#装饰者模式实例代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 装饰者 ...

  10. Servlet介绍以及简单实例

    一.背景介绍: HTTP:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.设计HTTP最初的目的是为了提供一种发布和接收 HTM ...