From 1efaa188770ddcf3cda62df5068fe28cb105ef37 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:48:19 +0800 Subject: [PATCH] =?UTF-8?q?Update=200105.=E6=9C=89=E5=90=91=E5=9B=BE?= =?UTF-8?q?=E7=9A=84=E5=AE=8C=E5=85=A8=E5=8F=AF=E8=BE=BE=E6=80=A7.md=20for?= =?UTF-8?q?=20java=20python=20and=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0105.有向图的完全可达性.md for java python and go --- .../0105.有向图的完全可达性.md | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/problems/kamacoder/0105.有向图的完全可达性.md b/problems/kamacoder/0105.有向图的完全可达性.md index 226d0f13..30baa649 100644 --- a/problems/kamacoder/0105.有向图的完全可达性.md +++ b/problems/kamacoder/0105.有向图的完全可达性.md @@ -289,10 +289,154 @@ int main() { ### Java +```java + +import java.util.*; + +public class Main { + + public static void dfs(List> graph, int key, boolean[] visited) { + for (int neighbor : graph.get(key)) { + if (!visited[neighbor]) { // Check if the next node is not visited + visited[neighbor] = true; + dfs(graph, neighbor, visited); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + List> graph = new ArrayList<>(); + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + graph.get(s).add(t); + } + + boolean[] visited = new boolean[n + 1]; + visited[1] = true; // Process node 1 beforehand + dfs(graph, 1, visited); + + for (int i = 1; i <= n; i++) { + if (!visited[i]) { + System.out.println(-1); + return; + } + } + System.out.println(1); + } +} + + +``` + + ### Python +``` python + +def dfs(graph, key, visited): + for neighbor in graph[key]: + if not visited[neighbor]: # Check if the next node is not visited + visited[neighbor] = True + dfs(graph, neighbor, visited) + +def main(): + import sys + input = sys.stdin.read + data = input().split() + + n = int(data[0]) + m = int(data[1]) + + graph = [[] for _ in range(n + 1)] + index = 2 + for _ in range(m): + s = int(data[index]) + t = int(data[index + 1]) + graph[s].append(t) + index += 2 + + visited = [False] * (n + 1) + visited[1] = True # Process node 1 beforehand + dfs(graph, 1, visited) + + for i in range(1, n + 1): + if not visited[i]: + print(-1) + return + + print(1) + +if __name__ == "__main__": + main() + + +``` + ### Go +```go + +package main + +import ( + "bufio" + "fmt" + "os" +) + +func dfs(graph [][]int, key int, visited []bool) { + visited[key] = true + for _, neighbor := range graph[key] { + if !visited[neighbor] { + dfs(graph, neighbor, visited) + } + } +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + var n, m int + fmt.Sscanf(scanner.Text(), "%d %d", &n, &m) + + graph := make([][]int, n+1) + for i := 0; i <= n; i++ { + graph[i] = make([]int, 0) + } + + for i := 0; i < m; i++ { + scanner.Scan() + var s, t int + fmt.Sscanf(scanner.Text(), "%d %d", &s, &t) + graph[s] = append(graph[s], t) + } + + visited := make([]bool, n+1) + + dfs(graph, 1, visited) + + for i := 1; i <= n; i++ { + if !visited[i] { + fmt.Println(-1) + return + } + } + fmt.Println(1) +} + + +``` + + ### Rust ### Javascript