HDU - 3836 Equivalent Sets (强连通分量+DAG)
题目大意:给出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)的更多相关文章
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- [tarjan] hdu 3836 Equivalent Sets
主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...
- hdu 3836 Equivalent Sets
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...
- hdu 3836 Equivalent Sets(强连通分量--加边)
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 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, ...
- hdu——3836 Equivalent Sets
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu 3836 tarjain 求强连通分量个数
// 给你一个有向图,问你最少加几条边能使得该图强连通 #include <iostream> #include <cstdio> #include <cstring&g ...
- hdu 4685 二分匹配+强连通分量
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...
随机推荐
- High-current supply uses standard three-terminal regulator
Voltage-regulator design for high output currents can be a critical and difficult task. Although vol ...
- Druid如何自动根据URL自动识别DriverClass的
Druid是根据url前缀来识别DriverClass的,这样使得配置更方便简洁. 前缀 DriverCLass 描述信息 jdbc:odps com.aliyun.odps.jdbc.OdpsDri ...
- vue2.0中引入UEditor的一些坑。。。。
开发后台系统的时候,富文本编辑器肯定是必不可少的,然后呢~在天朝当然要属百度编辑器(UEditor)最成熟了,功能全面,文档齐全(相对),ui优美(...,对于程序员来说)等等许多方面(MMP,还不是 ...
- dubbo知识点理解2
作者:网易云链接:https://www.zhihu.com/question/45413135/answer/226794957来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- VMware 11.0 简体中文版|附永久密钥
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...
- Android源码大放送之material design类型
本文转载自:http://www.apkbus.com/android-243232-1-1.html 鉴于大家对源码的渴望,就算自己辛苦一点也要满足大家的需求,查看了几百个源码之后终于筛选出了这些精 ...
- 周赛 POJ 3934 Queue
Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...
- 利用WMI检测电脑硬件信息,没办法显示cpu的信息
但你要给某些系统或软件加密时,需要了解到服务器的硬件信息时,系统和软件会利用WMI检测硬件信息, 而有时我们会遇到检测不到CPU的型号信息,如图 此时的解决方法: 1.确定“服务”里启动了WMI 2. ...
- java学习笔记14--多线程编程基础1
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...
- 【分布式计算】关于Hadoop、Spark、Storm的讨论
参考资料: 与 Hadoop 对比,如何看待 Spark 技术?:https://www.zhihu.com/question/26568496 还要不要做大数据:http://sinofool.cn ...