Submission #2214952
Source Code Expand
/**
* _ _ __ _ _ _ _ _ _ _
* | | | | / / | | (_) | (_) | | (_) | |
* | |__ __ _| |_ ___ ___ / /__ ___ _ __ ___ _ __ ___| |_ _| |_ ___ _____ ______ _ __ _ _ ___| |_ ______ ___ _ __ _ _ __ _ __ ___| |_ ___
* | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
* | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) | __/ |_| | |_| |\ V / __/ | | | |_| \__ \ |_ \__ \ | | | | |_) | |_) | __/ |_\__ \
* |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___| |_| \__,_|___/\__| |___/_| |_|_| .__/| .__/ \___|\__|___/
* | | | | | |
* |_| |_| |_|
*
* https://github.com/hatoo/competitive-rust-snippets
*/
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
use std::io::{stdin, stdout, BufWriter, StdoutLock};
use std::str::FromStr;
use std::fmt::Debug;
#[allow(dead_code)]
pub fn line() -> String {
let mut line: String = String::new();
stdin().read_line(&mut line).unwrap();
line.trim().to_string()
}
#[allow(dead_code)]
pub fn chars() -> Vec<char> {
line().chars().collect()
}
#[allow(dead_code)]
pub fn gets<T: FromStr>() -> Vec<T>
where
<T as FromStr>::Err: Debug,
{
let mut line: String = String::new();
stdin().read_line(&mut line).unwrap();
line.split_whitespace()
.map(|t| t.parse().unwrap())
.collect()
}
#[allow(dead_code)]
pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
let out = stdout();
let writer = BufWriter::new(out.lock());
f(writer)
}
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
use std::thread;
if BIG_STACK_SIZE {
thread::Builder::new()
.stack_size(32 * 1024 * 1024)
.name("solve".into())
.spawn(solve)
.unwrap()
.join()
.unwrap();
} else {
solve();
}
}
#[allow(dead_code)]
/// Union Find Tree
pub struct UFT {
pub par: Vec<usize>,
pub rank: Vec<usize>,
}
impl UFT {
#[allow(dead_code)]
pub fn new(n: usize) -> Self {
UFT {
par: (0..n).collect(),
rank: vec![0; n],
}
}
#[allow(dead_code)]
pub fn root(&mut self, x: usize) -> usize {
if self.par[x] == x {
x
} else {
let p = self.par[x];
let pp = self.root(p);
self.par[x] = pp;
pp
}
}
#[allow(dead_code)]
pub fn merge(&mut self, x: usize, y: usize) {
let x = self.root(x);
let y = self.root(y);
if x == y {
return;
}
if self.rank[x] < self.rank[y] {
self.par[x] = y;
} else {
self.par[y] = x;
if self.rank[x] == self.rank[y] {
self.rank[x] += 1;
}
}
}
}
#[derive(Eq, PartialEq, Clone, Debug)]
/// Equivalent to std::cmp::Reverse
pub struct Rev<T>(pub T);
impl<T: PartialOrd> PartialOrd for Rev<T> {
fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
other.0.partial_cmp(&self.0)
}
}
impl<T: Ord> Ord for Rev<T> {
fn cmp(&self, other: &Rev<T>) -> Ordering {
other.0.cmp(&self.0)
}
}
fn solve() {
let (n, m) = get!(usize, usize);
let ftc = get!(usize, usize, u64; m);
let q = get!(usize);
let ab = get!(usize, usize; q);
let mut g = vec![HashSet::new(); n];
for (f, t, c) in ftc {
g[f].insert((t, c));
g[t].insert((f, c));
}
let mut uft = UFT::new(n);
for (a, b) in ab {
uft.merge(a, b);
let root = uft.root(a);
let mut prim = HashSet::new();
prim.insert(a);
let mut heap: BinaryHeap<Rev<(u64, usize, usize)>> = BinaryHeap::new();
for &(to, c) in &g[a] {
if uft.root(to) == root {
heap.push(Rev((c, a, to)));
}
}
let mut sum = 0;
while let Some(Rev((cost, from, to))) = heap.pop() {
if !prim.contains(&to) {
prim.insert(to);
sum += cost;
for &(to2, c) in &g[to] {
if uft.root(to2) == root && !prim.contains(&to2) {
heap.push(Rev((c, to, to2)));
}
}
} else {
g[from].remove(&(to, cost));
g[to].remove(&(from, cost));
}
}
if (0..n).filter(|&i| uft.root(i) == root).count() != prim.len() {
println!("IMPOSSIBLE");
} else {
println!("{}", sum);
}
}
}
Submission Info
Submission Time |
|
Task |
F - 魔法の糸 |
User |
hatoo |
Language |
Rust (1.15.1) |
Score |
0 |
Code Size |
6932 Byte |
Status |
TLE |
Exec Time |
2107 ms |
Memory |
33020 KB |
Judge Result
Set Name |
All |
Score / Max Score |
0 / 200 |
Status |
|
Set Name |
Test Cases |
All |
00-sample1, 00-sample2, 01-random-small-tree01, 01-random-small-tree02, 02-random-small-sparse01, 02-random-small-sparse02, 02-random-small-sparse03, 03-random-small-dense01, 03-random-small-dense02, 03-random-small-dense03, 03-random-small-dense04, 03-random-small-dense05, 11-random-large-tree01, 11-random-large-tree02, 11-random-large-tree03, 12-random-large-sparse01, 12-random-large-sparse02, 12-random-large-sparse03, 13-random-large-denseA01, 13-random-large-denseA02, 13-random-large-denseA03, 13-random-large-denseA04, 14-random-large-denseB01, 14-random-large-denseB02, 14-random-large-denseB03, 14-random-large-denseB04, 15-random-large-denseC01, 15-random-large-denseC02, 20-kill-naive, 21-kill-loop, 23-min |
Case Name |
Status |
Exec Time |
Memory |
00-sample1 |
AC |
3 ms |
8572 KB |
00-sample2 |
AC |
3 ms |
8572 KB |
01-random-small-tree01 |
AC |
3 ms |
8572 KB |
01-random-small-tree02 |
AC |
3 ms |
8572 KB |
02-random-small-sparse01 |
AC |
4 ms |
8572 KB |
02-random-small-sparse02 |
AC |
4 ms |
8572 KB |
02-random-small-sparse03 |
AC |
4 ms |
8572 KB |
03-random-small-dense01 |
AC |
6 ms |
8572 KB |
03-random-small-dense02 |
AC |
6 ms |
8572 KB |
03-random-small-dense03 |
AC |
6 ms |
8572 KB |
03-random-small-dense04 |
AC |
6 ms |
8572 KB |
03-random-small-dense05 |
AC |
6 ms |
8572 KB |
11-random-large-tree01 |
AC |
94 ms |
10620 KB |
11-random-large-tree02 |
AC |
59 ms |
10620 KB |
11-random-large-tree03 |
AC |
90 ms |
10620 KB |
12-random-large-sparse01 |
AC |
505 ms |
10620 KB |
12-random-large-sparse02 |
AC |
500 ms |
10620 KB |
12-random-large-sparse03 |
AC |
499 ms |
10620 KB |
13-random-large-denseA01 |
AC |
1485 ms |
26876 KB |
13-random-large-denseA02 |
AC |
1454 ms |
26876 KB |
13-random-large-denseA03 |
AC |
1522 ms |
26876 KB |
13-random-large-denseA04 |
AC |
1491 ms |
26876 KB |
14-random-large-denseB01 |
AC |
1492 ms |
26876 KB |
14-random-large-denseB02 |
AC |
1469 ms |
26876 KB |
14-random-large-denseB03 |
AC |
1541 ms |
26876 KB |
14-random-large-denseB04 |
AC |
1507 ms |
26876 KB |
15-random-large-denseC01 |
AC |
1464 ms |
26876 KB |
15-random-large-denseC02 |
AC |
1424 ms |
26876 KB |
20-kill-naive |
AC |
1637 ms |
33020 KB |
21-kill-loop |
TLE |
2107 ms |
30972 KB |
23-min |
AC |
3 ms |
8572 KB |