-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestSumCycle.java
94 lines (78 loc) · 2.61 KB
/
LargestSumCycle.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
89
90
91
92
93
package Graph;
public class LargestSumCycle {
/*
public static long largesSumCycle(int N, int Edge[]){
int[] state = new int[N]; // 0 = unvisited, 1 = visiting, 2 = visited
int[] entryTime = new int[N]; // to track entry time for each node in the DFS path
int maxCycleSum = -1;
for (int i = 0; i < N; i++) {
if (state[i] == 0) {
maxCycleSum = Math.max(maxCycleSum, dfs(i, state, entryTime, Edge, 0));
}
}
return maxCycleSum;
}
private static int dfs(int node, int[] state, int[] entryTime, int[] Edge, int currentTime) {
state[node] = 1; // Mark as visiting
entryTime[node] = currentTime; // Record the entry time
int nextNode = Edge[node];
int maxCycleSum = -1;
if (nextNode != -1) {
if (state[nextNode] == 0) { // If the next node is unvisited
maxCycleSum = Math.max(maxCycleSum, dfs(nextNode, state, entryTime, Edge, currentTime + 1));
} else if (state[nextNode] == 1) { // If a cycle is detected
maxCycleSum = 0;
for (int i = nextNode; i != node; i = Edge[i]) {
maxCycleSum += i;
}
maxCycleSum += node;
}
}
state[node] = 2; // Mark as visited
return maxCycleSum;
}
*/
//######### Approach Second ##########
public static long res=-1;
public static long largesSumCycle2(int N, int Edge[]){
boolean[] vis=new boolean[N];
boolean[] pvis=new boolean[N];
for(int i=0;i<N;i++)
{
if(!vis[i])
{
dfs2(i,Edge,vis,pvis);
}
}
return res;
}
public static void dfs2(int i,int[] Edge,boolean[] vis,boolean[] pvis )
{
vis[i]=true;
pvis[i]=true;
if(Edge[i]!=-1)
{
int adj=Edge[i];
if(!vis[adj])
{
dfs2(adj,Edge,vis,pvis);
}
else if(pvis[adj])
{
int curr=adj;
long s=0;
do{
s+=curr;
curr=Edge[curr];
}while(curr!=adj);
res=Math.max(res,s);
}
}
pvis[i]=false;
}
public static void main(String[] args) {
int N = 4;
int Edge[] = {1, 2, 0, -1};
System.out.println( largesSumCycle2(N,Edge));
}
}