hdu-1827 Summer Holiday(強連通)

18244 ワード

http://acm.hdu.edu.cn/showproblem.php?pid=1827
ポイントを縮めた後に、統計の入度が0の点はいくつあって、それではこれらの点はすべて知らせなければならないので、しかしこれらの点も縮められたのかもしれなくて、だから毎回この点の所属の集合で1つの最小の値を探します。
  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 #include <string>
  8 #include <set>
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include 
 14 #include 
 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
", 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
",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) 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;jj) 135 y=min(y,p[bcc[i][j]]); 136 ans+=y; 137 } 138 } 139 //printf("%d
",j);
140 printf("%d %d
",x,ans); 141 } 142 return 0; 143 }
 
転載先:https://www.cnblogs.com/nowandforever/p/4729712.html