Skip to content

Commit e566b9f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b8d0755 commit e566b9f

File tree

1 file changed

+72
-72
lines changed

1 file changed

+72
-72
lines changed

content/3_Silver/Func_Graphs.mdx

+72-72
Original file line numberDiff line numberDiff line change
@@ -729,78 +729,78 @@ import java.io.*;
729729
import java.util.*;
730730

731731
public class Badge {
732-
static int[] ans;
733-
static int[] arr;
734-
public static void main(String[] args) throws IOException {
735-
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
736-
StringTokenizer st = new StringTokenizer(x.readLine());
737-
int n = Integer.parseInt(st.nextToken());
738-
arr = new int[n + 1];
739-
st = new StringTokenizer(x.readLine());
740-
for (int i = 1; i <= n; i++) {
741-
arr[i] = Integer.parseInt(st.nextToken());
742-
}
743-
744-
ans = new int[n + 1];
745-
Arrays.fill(ans, -1);
746-
for (int i = 1; i <= n; i++) {
747-
if (ans[i] == -1) { floyd(i); }
748-
}
749-
750-
StringBuilder output = new StringBuilder();
751-
for (int i = 1; i <= n; i++) { output.append(ans[i]).append(" "); }
752-
output.setLength(output.length() - 1);
753-
System.out.println(output);
754-
}
755-
756-
/**
757-
* fills up all vertices from curr to firstCycle,
758-
* whose answer has already been calculated
759-
*/
760-
static void fill_up(int curr, int first_cycle) {
761-
while (curr != first_cycle) {
762-
ans[curr] = ans[first_cycle];
763-
curr = arr[curr];
764-
}
765-
}
766-
767-
static int floyd(int curr) {
768-
int a = arr[curr];
769-
int b = arr[arr[curr]];
770-
// find cycle using Floyd's algo
771-
while (a != b) {
772-
/*
773-
* while finding cycle, if a node is found out to be already
774-
* calculated, go to the fillUp function, where, the answer from
775-
* vertex curr to this vertex a is ans[a]
776-
*/
777-
if (ans[a] != -1) {
778-
fill_up(curr, a);
779-
return 0;
780-
}
781-
a = arr[a];
782-
b = arr[arr[b]];
783-
}
784-
a = curr;
785-
while (a != b) {
786-
a = arr[a];
787-
b = arr[b];
788-
}
789-
int cycle_first = a;
790-
a = curr;
791-
792-
while (a != cycle_first) {
793-
ans[a] = cycle_first;
794-
a = arr[a];
795-
}
796-
a = cycle_first;
797-
// for each node in the cycle, the answer would be itself
798-
do {
799-
ans[a] = a;
800-
a = arr[a];
801-
} while (a != cycle_first);
802-
return 0;
803-
}
732+
static int[] ans;
733+
static int[] arr;
734+
public static void main(String[] args) throws IOException {
735+
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
736+
StringTokenizer st = new StringTokenizer(x.readLine());
737+
int n = Integer.parseInt(st.nextToken());
738+
arr = new int[n + 1];
739+
st = new StringTokenizer(x.readLine());
740+
for (int i = 1; i <= n; i++) {
741+
arr[i] = Integer.parseInt(st.nextToken());
742+
}
743+
744+
ans = new int[n + 1];
745+
Arrays.fill(ans, -1);
746+
for (int i = 1; i <= n; i++) {
747+
if (ans[i] == -1) { floyd(i); }
748+
}
749+
750+
StringBuilder output = new StringBuilder();
751+
for (int i = 1; i <= n; i++) { output.append(ans[i]).append(" "); }
752+
output.setLength(output.length() - 1);
753+
System.out.println(output);
754+
}
755+
756+
/**
757+
* fills up all vertices from curr to firstCycle,
758+
* whose answer has already been calculated
759+
*/
760+
static void fill_up(int curr, int first_cycle) {
761+
while (curr != first_cycle) {
762+
ans[curr] = ans[first_cycle];
763+
curr = arr[curr];
764+
}
765+
}
766+
767+
static int floyd(int curr) {
768+
int a = arr[curr];
769+
int b = arr[arr[curr]];
770+
// find cycle using Floyd's algo
771+
while (a != b) {
772+
/*
773+
* while finding cycle, if a node is found out to be already
774+
* calculated, go to the fillUp function, where, the answer from
775+
* vertex curr to this vertex a is ans[a]
776+
*/
777+
if (ans[a] != -1) {
778+
fill_up(curr, a);
779+
return 0;
780+
}
781+
a = arr[a];
782+
b = arr[arr[b]];
783+
}
784+
a = curr;
785+
while (a != b) {
786+
a = arr[a];
787+
b = arr[b];
788+
}
789+
int cycle_first = a;
790+
a = curr;
791+
792+
while (a != cycle_first) {
793+
ans[a] = cycle_first;
794+
a = arr[a];
795+
}
796+
a = cycle_first;
797+
// for each node in the cycle, the answer would be itself
798+
do {
799+
ans[a] = a;
800+
a = arr[a];
801+
} while (a != cycle_first);
802+
return 0;
803+
}
804804
}
805805
```
806806
</JavaSection>

0 commit comments

Comments
 (0)