HDU 1827 强连通 缩点 Summer Holiday
求出强连通分量,因为强连通中只要有一个人被通知到了,所有人都能被通知到。
缩点以后形成一个DAG,找出那些入度为0的点,累加上它们的权值就是答案。一个点的权值等于SCC中权值最小的那个点。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <stack>
- using namespace std;
- const int maxn = + ;
- int n, m;
- int w[maxn], cost[maxn];
- vector<int> G[maxn];
- stack<int> S;
- int pre[maxn], lowlink[maxn], sccno[maxn];
- int dfs_clock, scc_cnt;
- void dfs(int u)
- {
- pre[u] = lowlink[u] = ++dfs_clock;
- S.push(u);
- for(int i = ; i < G[u].size(); i++)
- {
- int v = G[u][i];
- if(!pre[v])
- {
- dfs(v);
- lowlink[u] = min(lowlink[u], lowlink[v]);
- }
- else if(!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]);
- }
- if(lowlink[u] == pre[u])
- {
- scc_cnt++;
- for(;;)
- {
- int x = S.top(); S.pop();
- sccno[x] = scc_cnt;
- if(x == u) break;
- }
- }
- }
- void find_scc()
- {
- dfs_clock = scc_cnt = ;
- memset(sccno, , sizeof(sccno));
- memset(pre, , sizeof(pre));
- for(int i = ; i <= n; i++) if(!pre[i]) dfs(i);
- }
- int indeg[maxn];
- int main()
- {
- while(scanf("%d%d", &n, &m) == )
- {
- for(int i = ; i <= n; i++) G[i].clear();
- for(int i = ; i <= n; i++) scanf("%d", w + i);
- while(m--)
- {
- int u, v; scanf("%d%d", &u, &v);
- G[u].push_back(v);
- }
- find_scc();
- memset(cost, 0x3f, sizeof(cost));
- for(int i = ; i <= n; i++) cost[sccno[i]] = min(cost[sccno[i]], w[i]);
- memset(indeg, , sizeof(indeg));
- for(int i = ; i <= n; i++)
- {
- for(int j = ; j < G[i].size(); j++)
- {
- int u = sccno[i], v = sccno[G[i][j]];
- if(u == v) continue;
- indeg[v]++;
- }
- }
- int ans1 = , ans2 = ;
- for(int i = ; i <= scc_cnt; i++) if(!indeg[i]) { ans1++; ans2 += cost[i]; }
- printf("%d %d\n", ans1, ans2);
- }
- return ;
- }
代码君
HDU 1827 强连通 缩点 Summer Holiday的更多相关文章
- hdu 3836 强连通+缩点:加边构强连通
#include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...
- hdu 2767 强连通缩点处理加边问题
#include <cstring> #include <cstdlib> #include <cstdio> 缩点的好处就是可以将乱七八糟的有向图 转化为无环的有 ...
- hdu 1827 有向图缩点看度数
题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...
- hdu 3072 强连通+缩点+最小树形图思想
#include<stdio.h> #include<string.h> #define N 51000 #define inf 1000000000 struct node ...
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- HDU 1827 Summer Holiday(强连通)
HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...
- HDU - 1827 Summer Holiday (强连通)
<题目链接> 题目大意: 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间 ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- 【HDU 5934】Bomb(强连通缩点)
Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...
随机推荐
- 关闭mysql validate-password插件
mysql5.7 的validate-password对密码策略有限制,比如长度大小写,太麻烦,我习惯开发环境下为root,所以在开发环境关闭这个插件的话只需在/etc/my.cnf中添加valida ...
- 通过Maven构建打包Spring boot,并将config配置文件提取到jar文件外
如果通过不同的IDE打包,着实会觉得依赖性太大,并且容易出现错误,操作也比较复杂 同时,spring-boot-maven-plugin的使用感觉,相关配置太少,并且无法满足方便部署和运行的需求. 这 ...
- 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here
你的飞碟在这儿 难度:☆ Code: #include<iostream> #include<cstring> #include<cstdio> using nam ...
- android dialog style属性设置
<!--最近做项目,用到alertDialog,用系统自带的style很难看,所以查了资料自己定义了个style. res/value/style.xml内增加以下代码:--> <s ...
- Burpsuite Professional安装及使用教程
转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...
- 宿主机Windows访问虚拟机Linux文件(一)
如果用户使用windows操作系统,但是在虚拟机下配置Linux内核操作操作系统,往往需要实现通过宿主机Windows操作系统访问Linux内核操作系统中资源.本次实验实现的是宿主机windows 1 ...
- Android商城开发系列(七)—— 使用RecyclerView展示首页数据
前面我们讲到了使用OkHttp请求网络和FastJson解析数据了,接下来我们就开始把获取到的数据通过数据适配器展示在页面上了.Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合 ...
- LibreOJ #107. 维护全序集
内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 splay模板题 屠龙宝刀点击就送 #include <cstdio&g ...
- Distributed Transaction Coordinator(DTC)一些问题的解决方法
有时运行某个程序或者安装SQL Server时报错. 错误信息: 事务管理器不可用.(从 HRESULT 异常: 0x8004D01B) 启动服务Distributed Transaction Coo ...
- python基础教程总结9——模块,包,标准库
1. 模块 在python中一个文件可以被看成一个独立模块,而包对应着文件夹,模块把python代码分成一些有组织的代码段,通过导入的方式实现代码重用. 1.1 模块搜索路径 导入模块时,是按照sys ...