This commit is contained in:
kkunkka
2025-09-25 11:31:22 +08:00
parent 3fb75cf088
commit 647d282067
2 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package com.dota.graph._2492;
import java.util.*;
class Solution {
public static void main(String[] args) {
new Solution().minScore(4, new int[][]{{1,2,2},{1,3,4},{3,4,7}});
}
int res = Integer.MAX_VALUE;
public int minScore(int n, int[][] roads) {
List<int[]>[] grid = new ArrayList[n+1];
Arrays.setAll(grid, i -> new ArrayList<>());
var visited = new boolean[n+1];
for (int[] road : roads) {
grid[road[0]].add(new int[]{road[1], road[2]});
grid[road[1]].add(new int[]{road[0], road[2]});
}
dfs(grid, 1, visited);
return res;
}
void dfs(List<int[]>[] grid, int i, boolean[] visited) {
visited[i] = true;
for (int k = 0; k < grid[i].size(); k++) {
res = Math.min(res, grid[i].get(k)[1]);
if (!visited[grid[i].get(k)[0]]) {
dfs(grid, grid[i].get(k)[0], visited);
}
}
}
}

View File

@@ -0,0 +1,43 @@
package com.dota.graph._3310;
import java.util.*;
class Solution {
public List<Integer> remainingMethods(int n, int k, int[][] invocations) {
var book = new boolean[n];
book[k] = true;
var g = new ArrayList[n];
Arrays.setAll(g, i -> new ArrayList());
for (int[] invocation : invocations) {
g[invocation[0]].add(invocation[1]);
}
df(g, k, book);
var res = new ArrayList<Integer>();
for (int[] invocation : invocations) {
if (!book[invocation[0]] && book[invocation[1]]) {
for (int i = 0; i < n; i++) {
res.add(i);
}
return res;
}
}
for (int i = 0; i < book.length; i++) {
if (!book[i]) {
res.add(i);
}
}
return res;
}
void df(List<Integer>[] g, int t, boolean[] book) {
book[t] = true;
for (Integer i : g[t]) {
if (!book[i]) {
df(g, i, book);
}
}
}
}