ABC 049, abc done, d is too slow and incorrect, need to learn more about union find
This commit is contained in:
25
atcoder/beginner_contests/abc049/Cargo.lock
generated
Normal file
25
atcoder/beginner_contests/abc049/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "abc049"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proconio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "proconio"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91c333be3af2936f8e810300bc74fe4d0cc168ebc04ab02a28c5b1060fa1bd59"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
12
atcoder/beginner_contests/abc049/Cargo.toml
Normal file
12
atcoder/beginner_contests/abc049/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "abc049"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# dependencies added to new project
|
||||
[dependencies]
|
||||
proconio = "0.4.3"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
panic = 'abort'
|
||||
13
atcoder/beginner_contests/abc049/src/bin/a.rs
Normal file
13
atcoder/beginner_contests/abc049/src/bin/a.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use proconio::input;
|
||||
|
||||
fn main(){
|
||||
input!{
|
||||
c: char,
|
||||
};
|
||||
|
||||
if c == 'u' || c == 'a' || c == 'o' || c == 'i' || c == 'e' {
|
||||
println!("vowel");
|
||||
} else {
|
||||
println!("consonant");
|
||||
}
|
||||
}
|
||||
14
atcoder/beginner_contests/abc049/src/bin/b.rs
Normal file
14
atcoder/beginner_contests/abc049/src/bin/b.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use proconio::input;
|
||||
|
||||
fn main(){
|
||||
input!{
|
||||
h: u32,
|
||||
_w: u32,
|
||||
c: [String; h],
|
||||
};
|
||||
|
||||
for s in c {
|
||||
println!("{}", s);
|
||||
println!("{}", s);
|
||||
}
|
||||
}
|
||||
39
atcoder/beginner_contests/abc049/src/bin/c.rs
Normal file
39
atcoder/beginner_contests/abc049/src/bin/c.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use proconio::input;
|
||||
|
||||
fn main(){
|
||||
input!{
|
||||
s: String,
|
||||
};
|
||||
|
||||
let mut cs : Vec<char> = s.chars().collect();
|
||||
while cs.len() >= 5 {
|
||||
let l = cs.len();
|
||||
|
||||
let lastfive : String = cs[l-5..].into_iter().collect();
|
||||
if lastfive == "dream" || lastfive == "erase" {
|
||||
cs = cs[..l-5].to_vec();
|
||||
} else if l >= 6 {
|
||||
let lastsix : String = cs[l-6..].into_iter().collect();
|
||||
if lastsix == "eraser" {
|
||||
cs = cs[..l-6].to_vec();
|
||||
} else if l >= 7 {
|
||||
let lastseven : String = cs[l-7..].into_iter().collect();
|
||||
if lastseven == "dreamer" {
|
||||
cs = cs[..l-7].to_vec();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if cs.len() > 0 {
|
||||
println!("NO");
|
||||
} else {
|
||||
println!("YES");
|
||||
}
|
||||
}
|
||||
76
atcoder/beginner_contests/abc049/src/bin/d.rs
Normal file
76
atcoder/beginner_contests/abc049/src/bin/d.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use proconio::input;
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
|
||||
fn dfs(start: usize, vertices: &[Vec<usize>]) -> HashSet<usize> {
|
||||
let mut result = HashSet::new();
|
||||
let mut visited = HashSet::new();
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(start);
|
||||
|
||||
while let Some(current_vertex) = queue.pop_front() {
|
||||
result.insert(current_vertex);
|
||||
|
||||
for neighbour in &vertices[current_vertex][..] {
|
||||
if visited.insert(*neighbour) {
|
||||
queue.push_front(*neighbour);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn main(){
|
||||
input!{
|
||||
n: usize,
|
||||
k: usize,
|
||||
l: usize,
|
||||
pq: [(usize, usize); k],
|
||||
rs: [(usize, usize); l],
|
||||
};
|
||||
|
||||
let mut roads = vec![Vec::new(); n];
|
||||
for (p, q) in pq {
|
||||
roads[p-1].push(q-1);
|
||||
}
|
||||
|
||||
let mut rails = vec![Vec::new(); n];
|
||||
for (r, s) in rs {
|
||||
rails[r-1].push(s-1);
|
||||
}
|
||||
|
||||
let mut city_found_road = Vec::new();
|
||||
let mut city_to_road_subsets = vec![-1; n];
|
||||
for city in 0..n {
|
||||
if city_to_road_subsets[city] == -1 {
|
||||
let connected_cities = dfs(city, &roads);
|
||||
let l = city_found_road.len();
|
||||
for connected_city in &connected_cities {
|
||||
city_to_road_subsets[*connected_city] = l as i32;
|
||||
}
|
||||
city_found_road.push(connected_cities);
|
||||
}
|
||||
}
|
||||
|
||||
let mut city_found_rails = Vec::new();
|
||||
let mut city_to_rail_subsets = vec![-1; n];
|
||||
for city in 0..n {
|
||||
if city_to_rail_subsets[city] == -1 {
|
||||
let connected_cities = dfs(city, &rails);
|
||||
let l = city_found_rails.len();
|
||||
for connected_city in &connected_cities {
|
||||
city_to_rail_subsets[*connected_city] = l as i32;
|
||||
}
|
||||
city_found_rails.push(connected_cities);
|
||||
}
|
||||
}
|
||||
|
||||
for city in 0..n {
|
||||
let rail_set = &city_found_rails[city_to_rail_subsets[city] as usize];
|
||||
let road_set = &city_found_road[city_to_road_subsets[city] as usize];
|
||||
|
||||
let result : HashSet<_> = rail_set.intersection(road_set).collect();
|
||||
|
||||
print!("{} ", result.len());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user