hdu - 1827 Summer Holiday (强连通)

http://acm.hdu.edu.cn/showproblem.php?pid=1827

缩点后,统计入度为0的点有多少个,那么这些点都是需要被通知的,但是这些点可能也是被缩的,所以每次在这个点所属集合找一个最小值即可.

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <string>
  8 #include <set>
  9 #include <functional>
 10 #include <numeric>
 11 #include <sstream>
 12 #include <stack>
 13 #include <map>
 14 #include <queue>
 15
 16 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 17
 18 #define ll long long
 19 #define inf 0x7f7f7f7f
 20 #define lc l,m,rt<<1
 21 #define rc m + 1,r,rt<<1|1
 22 #define pi acos(-1.0)
 23
 24 #define L(x)    (x) << 1
 25 #define R(x)    (x) << 1 | 1
 26 #define MID(l, r)   (l + r) >> 1
 27 #define Min(x, y)   (x) < (y) ? (x) : (y)
 28 #define Max(x, y)   (x) < (y) ? (y) : (x)
 29 #define E(x)        (1 << (x))
 30 #define iabs(x)     (x) < 0 ? -(x) : (x)
 31 #define OUT(x)  printf("%I64d\n", x)
 32 #define lowbit(x)   (x)&(-x)
 33 #define Read()  freopen("a.txt", "r", stdin)
 34 #define Write() freopen("dout.txt", "w", stdout);
 35
 36 using namespace std;
 37 #define N 1100
 38 //N为最大点数
 39 #define M 2100
 40 //M为最大边数
 41 int n, m;//n m 为点数和边数
 42
 43 struct Edge{
 44     int from, to, nex;
 45     bool sign;//是否为桥
 46 }edge[M<<1];
 47 int head[N], edgenum;
 48 void add(int u, int v){//边的起点和终点
 49     Edge E={u, v, head[u], false};
 50     edge[edgenum] = E;
 51     head[u] = edgenum++;
 52 }
 53
 54 int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
 55 int taj;//连通分支标号,从1开始
 56 int Belong[N];//Belong[i] 表示i点属于的连通分支
 57 bool Instack[N];
 58 vector<int> bcc[N]; //标号从1开始 每个强连通分量包含原图中的点
 59
 60 void tarjan(int u ,int fa){
 61     DFN[u] = Low[u] = ++ Time ;
 62     Stack[top ++ ] = u ;
 63     Instack[u] = 1 ;
 64
 65     for (int i = head[u] ; ~i ; i = edge[i].nex ){
 66         int v = edge[i].to ;
 67         if(DFN[v] == -1)
 68         {
 69             tarjan(v , u) ;
 70             Low[u] = min(Low[u] ,Low[v]) ;
 71             if(DFN[u] < Low[v])
 72             {
 73                 edge[i].sign = 1;//为割桥
 74             }
 75         }
 76         else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;
 77     }
 78     if(Low[u] == DFN[u]){
 79         int now;
 80         taj ++ ; bcc[taj].clear();
 81         do{
 82             now = Stack[-- top] ;
 83             Instack[now] = 0 ;
 84             Belong [now] = taj ;
 85             bcc[taj].push_back(now);
 86         }while(now != u) ;
 87     }
 88 }
 89
 90 void tarjan_init(int all){
 91     memset(DFN, -1, sizeof(DFN));
 92     memset(Instack, 0, sizeof(Instack));
 93     top = Time = taj = 0;
 94     for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意开始点标!!!
 95 }
 96 vector<int>G[N];
 97 int du[N];
 98 void suodian(){
 99     memset(du, 0, sizeof(du));
100     for(int i = 1; i <= taj; i++)G[i].clear();
101     for(int i = 0; i < edgenum; i++){
102         int u = Belong[edge[i].from], v = Belong[edge[i].to];
103         if(u!=v)
104         {
105             G[u].push_back(v), du[v]++;
106            // printf("%d %d\n",u,v);
107         }
108     }
109 }
110 void init(){memset(head, -1, sizeof(head)); edgenum=0;}
111 int p[N];
112 int main()
113 {
114     //Read();
115     int a,b;
116     while(~scanf("%d%d",&n,&m))
117     {
118         init();
119         for(int i=1;i<=n;i++) scanf("%d",&p[i]);
120         for(int i=0;i<m;i++)
121         {
122             scanf("%d%d",&a,&b);
123             add(a,b);
124         }
125         tarjan_init(n);
126         suodian();
127         int x=0,ans=0,y;
128         for(int i=1;i<=taj;++i)
129         {
130             y=1<<30;
131             if(du[i]==0) //出度为0点的个数
132             {
133                 x++;
134                 for(int j=0;j<bcc[i].size();++j)
135                     y=min(y,p[bcc[i][j]]);
136                 ans+=y;
137             }
138         }
139         //printf("%d\n",j);
140         printf("%d %d\n",x,ans);
141     }
142     return 0;
143 }
时间: 2024-10-18 14:29:13

hdu - 1827 Summer Holiday (强连通)的相关文章

HDU - 1827 Summer Holiday(强连通分量+贪心)

题目大意:To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. ―― William Blake 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话费了.他知道其他人也有一些别人

HDU 1827 Summer Holiday(强连通)

HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <stack> using namespace std; const int N = 1005; const int INF

HDU 1827 Summer Holiday (强连通分量)

题目地址:HDU 1827 先缩点,缩完点后,找出入度为0的块就是需要传递的块.然后用块中花费最少的来当代表块中的花费.累加起来就行了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #includ

hdu 1827 Summer Holiday tarjan+缩点

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2050    Accepted Submission(s): 939 Problem Description To see a World in a Gra

HDU 1827 Summer Holiday(Tarjan缩点)

Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. -- William Blake 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话

HDU 1827

 HDU - 1827 Summer Holiday Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. ―― William Blake

HDU 1827:Summer Holiday(强连通)

http://acm.hdu.edu.cn/showproblem.php?pid=1827 思路:强连通分量缩点后找入度为0的点,然后对于属于该强连通分量的找一个最小耗费的入口. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <cmath> 7 #i

[tarjan] 1827 Summer Holiday

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1721    Accepted Submission(s): 797 Problem Description To see a World in a

HDU 3639 Hawk-and-Chicken (强连通分量+树形DP)

题目地址:HDU 3639 先用强连通分量缩点,缩点之后,再重新按缩点之后的块逆序构图,每个块的值是里边缩的点的个数,那么得到选票的最大的一定是重新构图后入度为0的块,然后求出来找最大值即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>