2327. 知道秘密的人数

This commit is contained in:
li-chx 2025-09-09 01:43:27 +08:00
parent f82b9e1b8c
commit 5595c4bd16
1 changed files with 31 additions and 137 deletions

View File

@ -1,153 +1,47 @@
mod arr;
struct Solution;
use std::collections::HashMap;
use std::cmp::min;
impl Solution {
pub fn construct_grid_layout(n: i32, edges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
if n == 1 {
return vec![vec![0]];
pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
let mut last = 0_i64;
let mut forget_last = 0_i64;
let mut ans = 1_i64;
let modulo = 1_000_000_007;
fn get_range(cur: i32, n: i32, delay: i32, forget: i32) -> (usize, usize) {
let top = min(cur + forget - 1, n);
((cur + delay) as usize, top as usize)
}
let mut connections: HashMap<i32, Vec<i32>> = HashMap::new();
let mut degree: Vec<Vec<i32>> = vec![vec![]; 5];
for edge in &edges {
connections.entry(edge[0]).or_insert(vec![]).push(edge[1]);
connections.entry(edge[1]).or_insert(vec![]).push(edge[0]);
}
connections.iter().for_each(|(k, v)| {
degree[v.len()].push(*k);
});
if degree[1].len() != 0 {
let mut ans = vec![degree[1][0]];
let mut last = -1;
loop {
let next = connections[&ans[ans.len() - 1]]
.iter()
.find(|&&x| x != last)
.cloned();
if let Some(next) = next {
ans.push(next);
last = ans[ans.len() - 2];
} else {
break;
}
let mut dp = vec![0_i64; n as usize];
dp[0] = 1;
dp[1] = -1;
for i in 1..=n as usize {
let (start, end) = get_range(i as i32, n, delay, forget);
let cur = (last + dp[i - 1]) % modulo;
if end >= start {
ans = (ans + cur * ((1 + end - start) as i64 % modulo)) % modulo;
}
return vec![ans];
}
let d3 = degree[3].len() as f32;
let d4 = degree[4].len() as f32;
let delta = (d3 * d3) / 4.0 - 4.0 * d4;
if delta >= 0.0 {
let sqrt_delta = delta.sqrt();
let mut x = (d3 / 2.0 + sqrt_delta) / 2.0;
let mut y = (d3 / 2.0 - sqrt_delta) / 2.0;
if x > y {
std::mem::swap(&mut (x), &mut (y));
if i > forget as usize {
forget_last += dp[i - forget as usize - 1];
ans -= forget_last;
}
let x = x as usize + 2;
let y = y as usize + 2;
let mut ans = vec![vec![-1; x]; y];
ans[0][0] = degree[2][0];
for i in 0..y {
for j in 0..x {
if i == 0 && j == 0 {
if x == 2 {
if connections[&connections[&ans[0][0]][0]].len() == 2{
ans[0][1] = connections[&ans[0][0]][0];
ans[1][0] = connections[&ans[0][0]][1];
}
else {
ans[0][1] = connections[&ans[0][0]][1];
ans[1][0] = connections[&ans[0][0]][0];
}
continue;
}
let mut count_a = 1;
let mut cur = connections[&ans[i][j]][0];
let mut last = ans[i][j];
loop {
if connections[&cur].len() == 2 {
break;
}
let next = connections[&cur]
.iter()
.find(|&&x| x != last && (connections[&x].len() == 3 || connections[&x].len() == 2))
.cloned()
.unwrap();
count_a += 1;
last = cur;
cur = next;
}
let mut count_b = 1;
cur = connections[&ans[i][j]][1];
last = ans[i][j];
loop {
if connections[&cur].len() == 2 {
break;
}
let next = connections[&cur]
.iter()
.find(|&&x| x != last && (connections[&x].len() == 3 || connections[&x].len() == 2))
.cloned()
.unwrap();
count_b += 1;
last = cur;
cur = next;
}
if count_a < count_b {
ans[0][1] = connections[&ans[0][0]][0];
ans[1][0] = connections[&ans[0][0]][1];
}
else {
ans[0][1] = connections[&ans[0][0]][1];
ans[1][0] = connections[&ans[0][0]][0];
}
continue;
}
if ans[i][j] != -1 {
continue;
}
if i == 0 {
for node in &connections[&ans[i][j - 1]] {
if ans[0][j - 2] != *node {
if degree[3].contains(&node) || degree[2].contains(&node) {
ans[i][j] = *node;
} else {
ans[i + 1][j - 1] = *node;
}
}
}
} else {
let set_a = connections[&ans[i - 1][j]].clone();
let mut set_b = vec![];
for (k, l) in vec![(-1, 0), (1, 0), (0, -1), (0, 1)] {
let get_val = |i: i32, j: i32| -> i32 {
if i < 0 || j < 0 || i >= y as i32 || j >= x as i32 {
return -1;
}
ans[i as usize][j as usize]
};
set_b.push(get_val(i as i32 - 1 + k, j as i32 + l));
}
for node in &set_a {
if !set_b.contains(&node) {
ans[i][j] = *node;
}
}
}
}
if start - 1 < dp.len() {
dp[start - 1] += cur;
}
ans
} else {
edges
if end < dp.len() {
dp[end] -= cur;
}
last = cur;
}
while ans < 0 {
ans += modulo;
}
(ans % modulo) as i32
}
}
fn main() {
let matrix = arr::make_matrix(
"[[0,1],[0,11],[1,8],[1,9],[2,4],[2,8],[2,9],[3,5],[3,10],[4,6],[4,7],[5,6],[5,7],[6,9],[6,10],[9,11],[10,11]]",
);
let sl = Solution::construct_grid_layout(8, matrix);
let sl = Solution::people_aware_of_secret(6,2,4);
println!("{:?}", sl);
}