Last active
July 15, 2022 12:36
-
-
Save nattybear/0866e834ca0ee4d5ae21b6b55ae12857 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 4 | |
3 1 | |
3 2 | |
4 3 | |
5 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
static Map<Integer, List<Integer>> map; | |
static Map<Integer, Integer> dp; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
int m = sc.nextInt(); | |
map = new HashMap<>(); | |
dp = new HashMap<>(); | |
for (int i = 1; i <= n; i++) | |
map.put(i, new ArrayList<Integer>()); | |
for (int i = 0; i < m; i++) { | |
int a = sc.nextInt(); | |
int b = sc.nextInt(); | |
map.get(b).add(a); | |
} | |
List<Pair> list = new ArrayList<>(); | |
for (int i = 1; i <= n; i++) | |
list.add(new Pair(i, numberOfHacking(i))); | |
int max = list.stream() | |
.max((p1, p2) -> Integer.compare(p1.b, p2.b)) | |
.get() | |
.b; | |
list.stream() | |
.filter(p -> p.b == max) | |
.forEach(p -> System.out.print(p.a + " ")); | |
System.out.println("\b"); | |
} | |
static int numberOfHacking(int n) { | |
if (dp.get(n) != null) | |
return dp.get(n); | |
List<Integer> list = map.get(n); | |
if (list.size() == 0) | |
return 1; | |
else { | |
dp.put(n, 1 + list.stream().mapToInt(x -> numberOfHacking(x)).sum()); | |
return dp.get(n); | |
} | |
} | |
} | |
class Pair { | |
int a; | |
int b; | |
Pair(int a, int b) { | |
this.a = a; | |
this.b = b; | |
} | |
public String toString() { | |
return "(" + a + ", " + b + ")"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run: compile | |
@java Main < input.txt | |
compile: | |
@javac Main.java |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment