-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add atcoder/abc394/a.rs atcoder/abc394/b.rs atcoder/abc394/c.rs atcod…
…er/abc394/d.rs atcoder/abc394/e.rs atcoder/abc394/f.rs atcoder/abc394/g.rs
- Loading branch information
Showing
7 changed files
with
439 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fn getline() -> String { | ||
let mut ret = String::new(); | ||
std::io::stdin().read_line(&mut ret).ok().unwrap(); | ||
ret | ||
} | ||
|
||
fn main() { | ||
let mut ans = "".to_string(); | ||
for c in getline().chars() { | ||
if c == '2' { | ||
ans.push('2'); | ||
} | ||
} | ||
println!("{ans}"); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
fn getline() -> String { | ||
let mut ret = String::new(); | ||
std::io::stdin().read_line(&mut ret).ok().unwrap(); | ||
ret | ||
} | ||
|
||
fn main() { | ||
let n: usize = getline().trim().parse().unwrap(); | ||
let mut s = vec![]; | ||
for _ in 0..n { | ||
s.push(getline().trim().to_string()); | ||
} | ||
s.sort_by_key(|s| s.len()); | ||
let mut ans = "".to_string(); | ||
for s in s { | ||
ans += &s; | ||
} | ||
println!("{ans}"); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
fn getline() -> String { | ||
let mut ret = String::new(); | ||
std::io::stdin().read_line(&mut ret).ok().unwrap(); | ||
ret | ||
} | ||
|
||
fn main() { | ||
let mut ans = "".to_string(); | ||
let mut w = 0; | ||
for c in getline().trim().chars() { | ||
if c == 'W' { | ||
w += 1; | ||
} else if w > 0 && c == 'A' { | ||
ans.push('A'); | ||
for _ in 0..w { | ||
ans.push('C'); | ||
} | ||
w = 0; | ||
} else { | ||
for _ in 0..w { | ||
ans.push('W'); | ||
} | ||
w = 0; | ||
ans.push(c); | ||
} | ||
} | ||
for _ in 0..w { | ||
ans.push('W'); | ||
} | ||
println!("{ans}"); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
fn getline() -> String { | ||
let mut ret = String::new(); | ||
std::io::stdin().read_line(&mut ret).ok().unwrap(); | ||
ret | ||
} | ||
|
||
fn main() { | ||
let mut st = vec![]; | ||
for c in getline().trim().chars() { | ||
match c { | ||
'>' => { | ||
if let Some('<') = st.last() { | ||
st.pop(); | ||
} else { | ||
println!("No"); | ||
return; | ||
} | ||
}, | ||
')' => { | ||
if let Some('(') = st.last() { | ||
st.pop(); | ||
} else { | ||
println!("No"); | ||
return; | ||
} | ||
}, | ||
']' => { | ||
if let Some('[') = st.last() { | ||
st.pop(); | ||
} else { | ||
println!("No"); | ||
return; | ||
} | ||
}, | ||
_ => st.push(c), | ||
} | ||
} | ||
if st.is_empty() { | ||
println!("Yes"); | ||
} else { | ||
println!("No"); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::collections::*; | ||
use std::io::{Write, BufWriter}; | ||
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 | ||
macro_rules! input { | ||
($($r:tt)*) => { | ||
let stdin = std::io::stdin(); | ||
let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); | ||
let mut next = move || -> String{ | ||
bytes.by_ref().map(|r|r.unwrap() as char) | ||
.skip_while(|c|c.is_whitespace()) | ||
.take_while(|c|!c.is_whitespace()) | ||
.collect() | ||
}; | ||
input_inner!{next, $($r)*} | ||
}; | ||
} | ||
|
||
macro_rules! input_inner { | ||
($next:expr) => {}; | ||
($next:expr,) => {}; | ||
($next:expr, $var:ident : $t:tt $($r:tt)*) => { | ||
let $var = read_value!($next, $t); | ||
input_inner!{$next $($r)*} | ||
}; | ||
} | ||
|
||
macro_rules! read_value { | ||
($next:expr, [ $t:tt ; $len:expr ]) => { | ||
(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() | ||
}; | ||
($next:expr, chars) => { | ||
read_value!($next, String).chars().collect::<Vec<char>>() | ||
}; | ||
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); | ||
} | ||
|
||
fn main() { | ||
let out = std::io::stdout(); | ||
let mut out = BufWriter::new(out.lock()); | ||
macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} | ||
macro_rules! putvec { | ||
($v:expr) => { | ||
for i in 0..$v.len() { | ||
puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "}); | ||
} | ||
} | ||
} | ||
input! { | ||
n: usize, | ||
a: [chars; n], | ||
} | ||
let mut que = VecDeque::new(); | ||
for i in 0..n { | ||
que.push_back((0, i, i, '-')); | ||
} | ||
for i in 0..n { | ||
for j in 0..n { | ||
if a[i][j] != '-' { | ||
que.push_back((1, i, j, '-')); | ||
} | ||
} | ||
} | ||
const INF: i32 = 1 << 28; | ||
let mut dist = vec![vec![INF; n]; n]; | ||
let mut dist_half = vec![vec![[INF; 26]; n]; n]; | ||
while let Some((d, x, y, me)) = que.pop_front() { | ||
if me == '-' { | ||
if dist[x][y] <= d { | ||
continue; | ||
} | ||
dist[x][y] = d; | ||
for j in 0..n { | ||
if a[j][x] != '-' { | ||
que.push_back((d + 1, j, y, a[j][x])); | ||
} | ||
} | ||
} else { | ||
let idx = me as usize - 'a' as usize; | ||
if dist_half[x][y][idx] <= d { | ||
continue; | ||
} | ||
dist_half[x][y][idx] = d; | ||
for j in 0..n { | ||
if a[y][j] == me { | ||
que.push_back((d + 1, x, j, '-')); | ||
} | ||
} | ||
} | ||
} | ||
for i in 0..n { | ||
for v in &mut dist[i] { | ||
if *v == INF { | ||
*v = -1; | ||
} | ||
} | ||
putvec!(dist[i]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 | ||
macro_rules! input { | ||
($($r:tt)*) => { | ||
let stdin = std::io::stdin(); | ||
let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); | ||
let mut next = move || -> String{ | ||
bytes.by_ref().map(|r|r.unwrap() as char) | ||
.skip_while(|c|c.is_whitespace()) | ||
.take_while(|c|!c.is_whitespace()) | ||
.collect() | ||
}; | ||
input_inner!{next, $($r)*} | ||
}; | ||
} | ||
|
||
macro_rules! input_inner { | ||
($next:expr) => {}; | ||
($next:expr,) => {}; | ||
($next:expr, $var:ident : $t:tt $($r:tt)*) => { | ||
let $var = read_value!($next, $t); | ||
input_inner!{$next $($r)*} | ||
}; | ||
} | ||
|
||
macro_rules! read_value { | ||
($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; | ||
($next:expr, [ $t:tt ; $len:expr ]) => { | ||
(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() | ||
}; | ||
($next:expr, usize1) => (read_value!($next, usize) - 1); | ||
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); | ||
} | ||
|
||
fn main() { | ||
// In order to avoid potential stack overflow, spawn a new thread. | ||
let stack_size = 104_857_600; // 100 MB | ||
let thd = std::thread::Builder::new().stack_size(stack_size); | ||
thd.spawn(|| solve()).unwrap().join().unwrap(); | ||
} | ||
|
||
const INF: i32 = 1 << 28; | ||
|
||
fn dfs(v: usize, par: usize, g: &[Vec<usize>]) -> [i32; 2] { | ||
let is_ok = g[v].len() >= 4; | ||
let mut ma = -INF; | ||
let mut maj = vec![]; | ||
for &w in &g[v] { | ||
if w == par { | ||
continue; | ||
} | ||
let sub = dfs(w, v, g); | ||
if sub[1] > -INF { | ||
maj.push(sub[1]); | ||
} | ||
ma = ma.max(sub[0]); | ||
} | ||
let mut ret = [-INF; 2]; | ||
if is_ok { | ||
maj.sort(); | ||
maj.reverse(); | ||
let mut s = 0; | ||
for i in 0..3.min(maj.len()) { | ||
s += maj[i]; | ||
} | ||
ret[1] = s + 1; | ||
s = 0; | ||
for i in 0..4.min(maj.len()) { | ||
s += maj[i]; | ||
} | ||
ma = ma.max(s + 1); | ||
} | ||
ret[0] = ma; | ||
ret | ||
} | ||
|
||
fn solve() { | ||
input! { | ||
n: usize, | ||
ab: [(usize1, usize1); n - 1], | ||
} | ||
let mut g = vec![vec![]; n]; | ||
for &(a, b) in &ab { | ||
g[a].push(b); | ||
g[b].push(a); | ||
} | ||
let res = dfs(0, n, &g)[0]; | ||
if res == -INF { | ||
println!("-1"); | ||
} else { | ||
println!("{}", res * 3 + 2); | ||
} | ||
} |
Oops, something went wrong.