First use Tarjan to shrink the point, and then the point with the input degree of 0 must be selected at the same time is the minimum situation.
Summer Holiday
Time Limit: 10000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1098 accepted submission (s): 474
Problem descriptionto 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
I heard that lcy helped you book a 7-day tour of the new Mattel. wiskey was so happy that he could not stay at night. He wanted to tell you the news quickly, even though he had contact information for everyone, however, one contact once took too much time and telephone fees. He knows that others also have some contact information, so that he can notify others and ask others for help. Can you help wiskey calculate the minimum number of people to be notified, and the minimum number of phone bills to be paid can be notified to everyone?
Input Multiple groups of test arrays, ending with EOF.
The first line has two integers, N and M (1 <= n <= 1000, 1 <= m <= 2000), indicating the number of people and the contact logarithm.
There are n integers in the next line, indicating the phone charge for the person who contacted me by wiskey.
Then there are m rows. Each row has two integers x and y, indicating that X can be associated with y, but not y.
Output outputs the minimum number of contacts and the minimum cost.
Each case outputs a line of answers.
Sample input12 16
2 2 2 2 2 2 2 2 2 2 2
1 3
3 2
2 1
3 4
2 4
3 5
5 4
4 6
6 4
7 4
7 12
7 8
8 7
8 9
10 9
11 10
Sample output3 6
Author whisky
Sourcehdoj 2007 Summer exercise (3)-Hold by wiskey
Recommend whisky
# Include <stdio. h> # Include < String . H> # Include <Iostream> Using Namespace STD; # Define INF 0x3fffffff# Define N 1010 # Define M 2020 Int N, m; Struct Node { Int From , To, next;} edge [m]; Int Cos [N], low [N], vis [N], stack [N]; Int Nwcos [N]; Int Link [N]; Int Mark [N]; Int CNT, pre [N]; Int Nn, top; Int D [N]; Void Add_edge ( Int U, Int V) {edge [CNT]. From = U; edge [CNT]. = V; edge [CNT]. Next = Pre [u]; Pre [u] = CNT ++ ;} Void DFS ( Int S, Int K) {LOW [s] = K; vis [s] = K; stack [ ++ Top] = S; Mark [s] = 1 ; For ( Int P = pre [s]; P! =- 1 ; P =Edge [p]. Next ){ Int V = Edge [p].; If (MARK [v] = 0 ) DFS (v, k + 1 ); If (MARK [v] = 1 ) Low [s] = Min (low [v], low [s]);} If (Vis [s] = Low [s]) {NN ++ ; Int Mi = INF; Do { If (Cos [stack [Top] <mi) MI = Cos [stack [Top]; Link [stack [Top] = Nn; Mark [stack [Top] =- 1 ;} While (Stack [top --]! = S); nwcos [NN] = Mi ;}} Int Main (){ While (Scanf ( " % D " , & N, & M )! = EOF) {NN = 0 ; CNT = 0 ; Top = 0 ; Memset (PRE, - 1 , Sizeof (Pre); memset (mark, 0 , Sizeof (Mark); memset (D, 0 , Sizeof (D )); For ( Int I = 1 ; I <= N; I ++ ) Scanf ( " % D " ,& Cos [I]); For ( Int I = 0 ; I <m; I ++ ){ Int X, Y; scanf ( " % D " , & X ,& Y); add_edge (x, y );} For ( Int I = 1 ; I <= N; I ++ ){ If (MARK [I] = 0 ) DFS (I,0 );} For ( Int I = 0 ; I <CNT; I ++ ) {Edge [I]. From = Link [edge [I]. From ]; Edge [I]. = Link [edge [I]. To]; If (Edge [I].! = Edge [I]. From ) D [edge [I]. To] ++;} ////////////////////////////// // Int Num = 0 , ANS = 0 ; For ( Int I = 1 ; I <= nn; I ++ ){ If (D [I] = 0 ) {Num ++; Ans + = Nwcos [I] ;}} printf ( " % D \ n " , Num, ANS );} Return 0 ;}