-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10Java.java
61 lines (61 loc) · 1.71 KB
/
10Java.java
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
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
t--;
int n=sc.nextInt();
String[] a=new String[n];
long res=0;
sc.nextLine();
int[] v=new int[n+1];
long[] r=new long[n+1];
HashMap<Integer,ArrayList<Integer>> hm=new HashMap<>();
for(int i=0;i<n;i++)
{
a[i]=sc.nextLine();
//System.out.println(a[i]);
String[] s=a[i].split(" ");
v[Integer.parseInt(s[0])]=Integer.parseInt(s[1]);
ArrayList<Integer> list=new ArrayList<>();
for(int j=2;j<s.length;j++)
{
if(Integer.parseInt(s[j])!=0)
list.add(Integer.parseInt(s[j]));
}
hm.put(Integer.parseInt(s[0]),list);
}
for(int i=1;i<n;i++)
if(r[i]==0)
r[1]=find(i,v,r,hm,n);
for(int i=1;i<=n;i++)
{
res=Math.max(res,r[i]);
}
System.out.println(res);
}
}
public static long find(int i,int[] v,long[] r,HashMap<Integer,ArrayList<Integer>> hm,int n)
{
if(r[i]!=0)
{
//System.out.println(r[i]);
return r[i];
}
ArrayList<Integer> list=hm.get(i);
long ret=v[i];
long max=0;
for(int j: list)
{
long temp=find(j,v,r,hm,n);
if(temp>max)
max=temp;
}
r[i]=ret+max;
return r[i];
}
}