题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827

缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了。

  1. /*
  2. ━━━━━┒ギリギリ♂ eye!
  3. ┓┏┓┏┓┃キリキリ♂ mind!
  4. ┛┗┛┗┛┃\○/
  5. ┓┏┓┏┓┃ /
  6. ┛┗┛┗┛┃ノ)
  7. ┓┏┓┏┓┃
  8. ┛┗┛┗┛┃
  9. ┓┏┓┏┓┃
  10. ┛┗┛┗┛┃
  11. ┓┏┓┏┓┃
  12. ┛┗┛┗┛┃
  13. ┓┏┓┏┓┃
  14. ┃┃┃┃┃┃
  15. ┻┻┻┻┻┻
  16. */
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <cstring>
  21. #include <climits>
  22. #include <complex>
  23. #include <fstream>
  24. #include <cassert>
  25. #include <cstdio>
  26. #include <bitset>
  27. #include <vector>
  28. #include <deque>
  29. #include <queue>
  30. #include <stack>
  31. #include <ctime>
  32. #include <set>
  33. #include <map>
  34. #include <cmath>
  35. using namespace std;
  36. #define fr first
  37. #define sc second
  38. #define cl clear
  39. #define BUG puts("here!!!")
  40. #define W(a) while(a--)
  41. #define pb(a) push_back(a)
  42. #define Rint(a) scanf("%d", &a)
  43. #define Rll(a) scanf("%I64d", &a)
  44. #define Rs(a) scanf("%s", a)
  45. #define Cin(a) cin >> a
  46. #define FRead() freopen("in", "r", stdin)
  47. #define FWrite() freopen("out", "w", stdout)
  48. #define Rep(i, len) for(int i = 0; i < (len); i++)
  49. #define For(i, a, len) for(int i = (a); i < (len); i++)
  50. #define Cls(a) memset((a), 0, sizeof(a))
  51. #define Clr(a, x) memset((a), (x), sizeof(a))
  52. #define Full(a) memset((a), 0x7f7f, sizeof(a))
  53. #define pi 3.14159265359
  54. #define RT return
  55. #define lowbit(x) x & (-x)
  56. #define onenum(x) __builtin_popcount(x)
  57. typedef long long LL;
  58. typedef long double LD;
  59. typedef unsigned long long ULL;
  60. typedef pair<int, int> pii;
  61. typedef pair<string, int> psi;
  62. typedef map<string, int> msi;
  63. typedef vector<int> vi;
  64. typedef vector<LL> vl;
  65. typedef vector<vl> vvl;
  66. typedef vector<bool> vb;
  67.  
  68. const int maxn = ;
  69. const int maxm = ;
  70. const int inf = 0x7f7f7f;
  71. int n, m;
  72. vector<int> G[maxn];
  73. int vis[maxn][maxn];
  74. int w[maxn];
  75. int dfn[maxn], low[maxn], idx;
  76. int st[maxn], top;
  77. int belong[maxn], bcnt;
  78. int in[maxn], out[maxn];
  79. int cost[maxn];
  80.  
  81. void tarjan(int u) {
  82. int v = u;
  83. dfn[u] = low[u] = ++idx;
  84. vis[][u] = ; st[++top] = u;
  85. Rep(i, G[u].size()) {
  86. v = G[u][i];
  87. if(!dfn[v]) {
  88. tarjan(v);
  89. low[u] = min(low[u], low[v]);
  90. }
  91. else if(vis[][v]) low[u] = min(low[u], dfn[v]);
  92. }
  93. if(low[u] == dfn[u]) {
  94. bcnt++;
  95. do {
  96. v = st[top--];
  97. vis[][v] = ;
  98. belong[v] = bcnt;
  99. } while(u != v);
  100. }
  101. }
  102.  
  103. int main() {
  104. // FRead();
  105. int u, v;
  106. while(~Rint(n) && ~Rint(m)) {
  107. Cls(dfn); Cls(low); Cls(w);
  108. Cls(st); Cls(vis); top = ;
  109. Cls(belong); bcnt = ; idx = ;
  110. Cls(in); Cls(out);
  111. For(i, , n+) {
  112. Rint(w[i]);
  113. G[i].cl();
  114. cost[i] = inf;
  115. }
  116. Rep(i, m) {
  117. Rint(u); Rint(v);
  118. G[u].pb(v);
  119. }
  120. For(i, , n+) if(!dfn[i]) tarjan(i);
  121. Cls(vis);
  122. For(u, , n+) {
  123. cost[belong[u]] = min(cost[belong[u]], w[u]);
  124. Rep(i, G[u].size()) {
  125. int v = G[u][i];
  126. if(belong[u] == belong[v]) continue;
  127. in[belong[v]]++;
  128. out[belong[u]]++;
  129. }
  130. }
  131. int ret1 = , ret2 = ;
  132. For(i, , bcnt+) {
  133. if(in[i] == ) {
  134. ret1++;
  135. ret2 += cost[i];
  136. }
  137. }
  138. printf("%d %d\n", ret1, ret2);
  139. }
  140. RT ;
  141. }

[HDOJ1827]Summer Holiday(强连通分量,缩点)的更多相关文章

  1. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  2. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

  3. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

  4. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  5. ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)

    题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...

  6. POJ2553 The Bottom of a Graph(强连通分量+缩点)

    题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...

  7. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  8. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

  9. tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)

    这篇文章是从网络上总结各方经验 以及 自己找的一些例题的算法模板,主要是用于自己的日后的模板总结以后防失忆常看看的, 写的也是自己能看懂即可. tarjan算法的功能很强大, 可以用来求解强连通分量, ...

  10. LA 4287 等价性证明(强连通分量缩点)

    https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...

随机推荐

  1. String对象中常用的方法

    String对象中常用的方法   1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...

  2. margin-top相对谁的问题

    根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边距重叠.意思便是:如果你只想margin相对于父标 ...

  3. .net 自然排序方式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. mysql中实现oracle中的rowid功能

    mysql中没有函数实现,只能自己手动添加变量递增  := 就是赋值,只看红色字体就行 select @rownum:=@rownum+1,img.img_path,sku.sku_name from ...

  5. .Xresources 配置文件

    安装rxvt-unicode-256color,如果不是这个版本的话VIM配色会显示不正常. ~/.Xresources配置文件如下 !urxvt color scheme: URxvt*backgr ...

  6. [转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测

    Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大 ...

  7. 1021 玛丽卡 - Wikioi

    题目描述 Description麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知 ...

  8. 1069: [SCOI2007]最大土地面积 - BZOJ

    Description 在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成的多边形面积最大.Input 第1行一个正整数N,接下来N行,每行2个数x,y, ...

  9. IntelliJ IDEA 比较当前版本文件与历史文件

    前言: 写代码修改后怎样比较与历史文件的区别呢?idea提供了2种比较方式(目前笔者所了解到的) 一.SVN的版本比较 二.当前文件与历史版本比较

  10. shell 循环使用

    问题描述:                  shell中for循环while循环的使用 问题解决:              (1)for循环                      (1.1)数 ...