-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeeks.java
88 lines (80 loc) · 2.38 KB
/
geeks.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
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class geeks {
public static void main(String[] args) {
int N = 5;
int[][]edges= {{1, 2},
{1, 3},
{2, 4},
{2, 5},
{3, 6}};
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int i=0; i<N; i++){
ArrayList<Integer> ed = new ArrayList<>();
ed.add(edges[i][0]);
ed.add(edges[i][1]);
list.add(ed);
}
geeks o = new geeks();
int ans = o.maxPoints(list);
System.out.println(ans);
}
int maxPoints(ArrayList<ArrayList<Integer>> edges)
{
//code here.
if(edges.isEmpty())return 1;
HashMap<Integer,ArrayList<Integer>> graph = new HashMap<>();
for (ArrayList<Integer> e : edges) {
int v1 = e.get(0);
int v2 = e.get(1);
if (!graph.containsKey(v1)) {
graph.put(v1, new ArrayList<>());
}
if (!graph.containsKey(v2)) {
graph.put(v2, new ArrayList<>());
}
graph.get(v1).add(v2);
graph.get(v2).add(v1);
}
int points = Integer.MIN_VALUE;
for (Map.Entry<Integer, ArrayList<Integer>> set : graph.entrySet()) {
if(set.getValue().size()==1) {
int p;
p = solution(graph, set.getKey(),-1).point;
points = Math.max(points, p);
}
}
return points;
}
int totalScore=0;
class Pair{
int point;
int unVisited;
}
HashMap<Integer,Pair> dp = new HashMap<>();
Pair solution(HashMap<Integer, ArrayList<Integer>> graph,int src, int parent) {
int dir = src*10000 + parent;
if(dp.containsKey(dir)){
return dp.get(dir);
}
ArrayList<Integer> nbrs = graph.get(src);
int unVisited=0;
int point=0;
for(int nbr:nbrs){
if(nbr!=parent) {
Pair p;
p = solution(graph, nbr,src);
unVisited+=p.unVisited;
point+=p.point;
}
}
point+= unVisited+1;
Pair res=new Pair();
res.point=point;
res.unVisited=unVisited+1;
dp.put(dir,res);
return res;
}
}