求出强连通分量,因为强连通中只要有一个人被通知到了,所有人都能被通知到。

缩点以后形成一个DAG,找出那些入度为0的点,累加上它们的权值就是答案。一个点的权值等于SCC中权值最小的那个点。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <vector>
  6. #include <stack>
  7. using namespace std;
  8.  
  9. const int maxn = + ;
  10.  
  11. int n, m;
  12. int w[maxn], cost[maxn];
  13. vector<int> G[maxn];
  14.  
  15. stack<int> S;
  16. int pre[maxn], lowlink[maxn], sccno[maxn];
  17. int dfs_clock, scc_cnt;
  18.  
  19. void dfs(int u)
  20. {
  21. pre[u] = lowlink[u] = ++dfs_clock;
  22. S.push(u);
  23. for(int i = ; i < G[u].size(); i++)
  24. {
  25. int v = G[u][i];
  26. if(!pre[v])
  27. {
  28. dfs(v);
  29. lowlink[u] = min(lowlink[u], lowlink[v]);
  30. }
  31. else if(!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]);
  32. }
  33. if(lowlink[u] == pre[u])
  34. {
  35. scc_cnt++;
  36. for(;;)
  37. {
  38. int x = S.top(); S.pop();
  39. sccno[x] = scc_cnt;
  40. if(x == u) break;
  41. }
  42. }
  43. }
  44.  
  45. void find_scc()
  46. {
  47. dfs_clock = scc_cnt = ;
  48. memset(sccno, , sizeof(sccno));
  49. memset(pre, , sizeof(pre));
  50. for(int i = ; i <= n; i++) if(!pre[i]) dfs(i);
  51. }
  52.  
  53. int indeg[maxn];
  54.  
  55. int main()
  56. {
  57. while(scanf("%d%d", &n, &m) == )
  58. {
  59. for(int i = ; i <= n; i++) G[i].clear();
  60. for(int i = ; i <= n; i++) scanf("%d", w + i);
  61. while(m--)
  62. {
  63. int u, v; scanf("%d%d", &u, &v);
  64. G[u].push_back(v);
  65. }
  66.  
  67. find_scc();
  68.  
  69. memset(cost, 0x3f, sizeof(cost));
  70. for(int i = ; i <= n; i++) cost[sccno[i]] = min(cost[sccno[i]], w[i]);
  71.  
  72. memset(indeg, , sizeof(indeg));
  73. for(int i = ; i <= n; i++)
  74. {
  75. for(int j = ; j < G[i].size(); j++)
  76. {
  77. int u = sccno[i], v = sccno[G[i][j]];
  78. if(u == v) continue;
  79. indeg[v]++;
  80. }
  81. }
  82.  
  83. int ans1 = , ans2 = ;
  84. for(int i = ; i <= scc_cnt; i++) if(!indeg[i]) { ans1++; ans2 += cost[i]; }
  85. printf("%d %d\n", ans1, ans2);
  86. }
  87.  
  88. return ;
  89. }

代码君

HDU 1827 强连通 缩点 Summer Holiday的更多相关文章

  1. hdu 3836 强连通+缩点:加边构强连通

    #include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...

  2. hdu 2767 强连通缩点处理加边问题

    #include <cstring> #include <cstdlib> #include <cstdio> 缩点的好处就是可以将乱七八糟的有向图 转化为无环的有 ...

  3. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  4. hdu 3072 强连通+缩点+最小树形图思想

    #include<stdio.h> #include<string.h> #define N 51000 #define inf 1000000000 struct node ...

  5. hdu 4635 Strongly connected 强连通缩点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...

  6. HDU 1827 Summer Holiday(强连通)

    HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...

  7. HDU - 1827 Summer Holiday (强连通)

    <题目链接> 题目大意: 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间 ...

  8. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  9. 【HDU 5934】Bomb(强连通缩点)

    Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...

随机推荐

  1. 关闭mysql validate-password插件

    mysql5.7 的validate-password对密码策略有限制,比如长度大小写,太麻烦,我习惯开发环境下为root,所以在开发环境关闭这个插件的话只需在/etc/my.cnf中添加valida ...

  2. 通过Maven构建打包Spring boot,并将config配置文件提取到jar文件外

    如果通过不同的IDE打包,着实会觉得依赖性太大,并且容易出现错误,操作也比较复杂 同时,spring-boot-maven-plugin的使用感觉,相关配置太少,并且无法满足方便部署和运行的需求. 这 ...

  3. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here

    你的飞碟在这儿 难度:☆ Code: #include<iostream> #include<cstring> #include<cstdio> using nam ...

  4. android dialog style属性设置

    <!--最近做项目,用到alertDialog,用系统自带的style很难看,所以查了资料自己定义了个style. res/value/style.xml内增加以下代码:--> <s ...

  5. Burpsuite Professional安装及使用教程

    转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...

  6. 宿主机Windows访问虚拟机Linux文件(一)

    如果用户使用windows操作系统,但是在虚拟机下配置Linux内核操作操作系统,往往需要实现通过宿主机Windows操作系统访问Linux内核操作系统中资源.本次实验实现的是宿主机windows 1 ...

  7. Android商城开发系列(七)—— 使用RecyclerView展示首页数据

    前面我们讲到了使用OkHttp请求网络和FastJson解析数据了,接下来我们就开始把获取到的数据通过数据适配器展示在页面上了.Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合 ...

  8. LibreOJ #107. 维护全序集

    内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名   splay模板题 屠龙宝刀点击就送 #include <cstdio&g ...

  9. Distributed Transaction Coordinator(DTC)一些问题的解决方法

    有时运行某个程序或者安装SQL Server时报错. 错误信息: 事务管理器不可用.(从 HRESULT 异常: 0x8004D01B) 启动服务Distributed Transaction Coo ...

  10. python基础教程总结9——模块,包,标准库

    1. 模块 在python中一个文件可以被看成一个独立模块,而包对应着文件夹,模块把python代码分成一些有组织的代码段,通过导入的方式实现代码重用. 1.1 模块搜索路径 导入模块时,是按照sys ...