import java util ArrayList import java util Arrays import java util Sc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.Math;
class point {
int min;
int IsParent=0;
int mark;
int value;
int cap=0;
ArrayList<Integer> list = new ArrayList<Integer>();
}
public class Factorial {
private static int count=0;
private static int number=0;
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
int i=0,n=sc.nextInt(),m=sc.nextInt(),first,second,answer=0;
point [] graph=new point[n];
for (i=0;i<n;i++) {
graph[i]=new point();
graph[i].value=sc.nextInt();
}
for(i=0;i<m;i++){
first=sc.nextInt()-1; second=sc.nextInt()-1;
graph[first].cap++;
graph[first].list.add(second);
}
for(i=0;i<n;i++){
if(graph[i].mark==0){
graph[i].IsParent=1;
dfs1(graph,graph[i]);
}
}
for(i=0;i<n;i++){
graph[i].mark=0;
}
for(i=0;i<n;i++){
if(graph[i].IsParent==1){
dfs2(graph,graph[i]);
if(count%number!=0){
graph[i].min=(count/number)+1;
}
else{
graph[i].min=count/number;
}
count=0;
number=0;
}
}
for(i=0;i<n;i++){
if(graph[i].IsParent==1 && graph[i].min>answer)
answer=graph[i].min;
}
System.out.println(answer);
}
public static void dfs1(point [] graph,point Vertex){
int i, size;
point v=Vertex;
point u;
v.mark=1;
for (i = 0; i < Vertex.cap; i++) {
u = graph[Vertex.list.get(i)];
if(u.IsParent==1)
u.IsParent=0;
if(u.mark==0) {
dfs1(graph,u);
}
//действия при проходе по ребру
}
v.mark=2;
}
public static void dfs2(point [] graph,point Vertex){
count+=Vertex.value;
number++;
int i, size;
point v=Vertex;
point u;
v.mark=1;
for (i = 0; i < Vertex.cap; i++) {
u = graph[Vertex.list.get(i)];
if(u.IsParent==1)
u.IsParent=0;
if(u.mark==0) {
dfs2(graph,u);
}
//действия при проходе по ребру
}
v.mark=2;
}
}