2022-11-04:給定一個正數n,表示有多少個節點
給定一個二維數組edges,表示所有無向邊
edges[i] = {a, b} 表示a到b有一條無向邊
edges一定表示得是一個無環無向圖,也就是樹結構
每個節點可以染1、2、3三種顏色。
要求 : 非葉節點得相鄰點一定要至少有兩種和自己不同顏色得點。
返回一種達標得染色方案,也就是一個數組,表示每個節點得染色狀況。
1 <= 節點數量 <= 10得5次方。
來自米哈游。
答案2022-11-04:
生成圖,選一個頭節點,深度優先染色。
代碼用rust編寫。代碼如下:
use std::{iter::repeat, vec};use rand::Rng;fn main() { let nn: i32 = 100; let test_time: i32 = 1000; println!("測試開始"); for i in 0..test_time { let n = rand::thread_rng().gen_range(0, nn) + 1; let mut edges = random_edges(n); let mut ans = dye(n, &mut edges); if !right_answer(n, &mut edges, &mut ans) { println!("出錯了!{}", i); break; } } println!("測試結束");}// 1 2 3 1 2 3 1 2 3const RULE1: [i32; 3] = [1, 2, 3];// // 1 3 2 1 3 2 1 3 2const RULE2: [i32; 3] = [1, 3, 2];fn dye(n: i32, edges: &mut Vec<Vec<i32>>) -> Vec<i32> { let mut graph: Vec<Vec<i32>> = vec![]; // 0 : { 2, 1 } // 1 : { 0 } // 2 : { 0 } for _i in 0..n { graph.push(vec![]); } for edge in edges.iter() { // 0 -> 2 // 1 -> 0 graph[edge[0] as usize].push(edge[1]); graph[edge[1] as usize].push(edge[0]); } // 選一個頭節點! let mut head = -1; for i in 0..n { if graph[i as usize].len() >= 2 { head = i; break; } } // graph // head let mut colors: Vec<i32> = repeat(0).take(n as usize).collect(); if head == -1 { // 兩個點,互相連一下 // 把colors,所有位置,都設置成1 colors = repeat(1).take(n as usize).collect(); } else { // dfs 染色了! colors[head as usize] = 1; let h = graph[head as usize][0]; dfs(&mut graph, h, 1, &RULE1, &mut colors); for i in 1..graph[head as usize].len() as i32 { let h = graph[head as usize][i as usize]; dfs(&mut graph, h, 1, &RULE2, &mut colors); } } return colors;}// 整個圖結構,都在graph// 當前來到得節點,是head號節點// head號節點,在level層// 染色得規則,rule {1,2,3...} {1,3,2...}// 做得事情:以head為頭得整顆樹,每個節點,都染上顏色// 填入到colors數組里去fn dfs(graph: &mut Vec<Vec<i32>>, head: i32, level: i32, rule: &[i32; 3], colors: &mut Vec<i32>) { colors[head as usize] = rule[(level % 3) as usize]; for next in graph[head as usize].clone().iter() { if colors[*next as usize] == 0 { dfs(graph, *next, level + 1, rule, colors); } }}// 為了測試// 生成無環無向圖fn random_edges(n: i32) -> Vec<Vec<i32>> { let mut order: Vec<i32> = repeat(0).take(n as usize).collect(); for i in 0..n { order[i as usize] = i; } let mut i = n - 1; while i >= 0 { order.swap(i as usize, rand::thread_rng().gen_range(0, i + 1) as usize); i -= 1; } let mut edges: Vec<Vec<i32>> = repeat(repeat(0).take(2).collect()) .take((n - 1) as usize) .collect(); for i in 1..n { edges[(i - 1) as usize][0] = order[i as usize]; edges[(i - 1) as usize][1] = order[rand::thread_rng().gen_range(0, i) as usize]; } return edges;}// 為了測試fn right_answer(n: i32, edges: &mut Vec<Vec<i32>>, colors: &mut Vec<i32>) -> bool { let mut graph: Vec<Vec<i32>> = vec![]; for _i in 0..n { graph.push(vec![]); } for edge in edges.iter() { graph[edge[0] as usize].push(edge[1]); graph[edge[1] as usize].push(edge[0]); } let mut has_colors: Vec<bool> = repeat(false).take(4).collect(); let mut i = 0; let mut color_cnt = 1; while i < n { if colors[i as usize] == 0 { return false; } if graph[i as usize].len() <= 1 { // i號點是葉節點 i += 1; color_cnt = 1; continue; } has_colors[colors[i as usize] as usize] = true; for near in graph[i as usize].iter() { if !has_colors[colors[*near as usize] as usize] { has_colors[colors[*near as usize] as usize] = true; color_cnt += 1; } } if color_cnt != 3 { return false; } has_colors = repeat(false).take(4).collect(); i += 1; color_cnt = 1; } return true;}
執行結果如下:
***
[左神java代碼](感謝分享github感謝原創分享者/algorithmzuo/weekly-problems/blob/main/src/class_2022_08_2_week/Code02_TreeDye.java)